avadev/AvaTax-REST-V2-JS-SDK

Error parsing HTML as JSON

brunoabreu opened this issue · 3 comments

When API is down the server responds an HTML, then an unfriendly error explodes when trying to parse response as JSON here:

.then(res => res.json())

Error

{"error":{"message":"Unexpected token < in JSON at position 0","stack":"SyntaxError: Unexpected token < in JSON at position 0\n at JSON.parse ()\n at /node_modules/node-fetch/lib/body.js:48:1\n at \n at process._tickCallback (internal/process/next_tick.js:188:7)","url":null,"method":null,"data":null}}

HTML

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8" />
    <title>500 Internal Server Error</title>
    <style type="text/css">
        body {
            background-color: white;
            color: #111111;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 2em 4em;
        }

        footer a {
            color: darkblue;
            text-decoration: none;
            font-weight: bolder;
        }

        #header {
            margin-bottom: 2.5em;
        }

        .stacktrace pre {
            display: inline;
        }

        .faded {
            color: #999999;
            font-weight: normal;
        }

        div.message {
            margin-top: 2.5em;
            padding: 0.3em 1em;
            border-left: 0.25em solid red;
        }

        .light {
            font-size: 1.3em;
            font-weight: lighter;
        }

        .heavy {
            font-size: 1.5em;
        }

        .exception {
            color: red;
        }

        .stacktrace {
            padding-top: 0.3em;
            padding-left: 2em;
            display: block;
            font-weight: bold;
        }

        .codeSnippet {
            margin-left: 2em;
            margin-top: 1em;
            margin-bottom: 1em;
            display: inline-block;
            border-top: 0.2em solid #cccccc;
            border-bottom: 0.2em solid #cccccc;
            color: black;
        }

        .codeSnippet div:nth-of-type(2n) {
            background-color: #f0f0f0;
        }

        .codeSnippet div:nth-of-type(2n + 1) {
            background-color: #f6f6f6;
        }

        .codeSnippet div.filename {
            font-weight: bold;
            background-color: white;
            margin: 0.6em;
        }

        .codeSnippet div.line {
            padding: 0.2em;
            line-height: 1em;
        }

        .codeSnippet div.line .line-number {
            color: #999999;
            text-align: right;
            margin-right: 0.5em;
        }

        .codeSnippet div.error {
            color: red;
            font-weight: bolder;
            background-color: #ffeda7;
        }

        .codeSnippet code {
            white-space: pre;
        }

        .rawExceptionBlock {
            margin-top: 1em;
            margin-left: 1em;
        }

        #rawException {
            display: none;
        }

        footer {
            margin-top: 2em;
            font-size: smaller;
            font-weight: lighter;
        }
    </style>
    <script type="text/javascript">
        function showRawException() {
            var div = document.getElementById('rawException');
            div.style.display = 'inline-block';
            div.scrollIntoView(true);
        }
    </script>
</head>
<body>

    <div id="header">
        <div style="font-size: 6em; display: inline-block;">
            :(
        </div>
        <div style="display: inline-block; padding-left: 3em;">
            <span style="font-size: 2em;">Oops.</span><br />
            <span style="font-size: 1.65em; font-weight: lighter;">500 Internal Server Error</span>
        </div>
    </div>

    <div class="message">
    <span class="heavy">An error occurred while starting the application.</span><br />
</div>
</body>

Hi @brunoabreu
Thank you for discovering this issue, I and my team are working to find the cause and solution to this. Do you mind provide some additional info? (Were you able to establish a connection to the API at the time of error? What endpoint were you calling?)
Thanks,
Han from Avalara

Hi @brunoabreu,

Thanks for bringing this to our attention. It seems the SDK parses responses as JSON regardless of content type. We've identified the cause of the issue and are currently working to resolve it. We'll keep you posted on our progress!

Thanks,
Genevieve from Avalara

Hi @brunoabreu,

With the new 18.9 update, we've fixed the parsing issue on our end. I'm closing this issue, but if you have any trouble in the future, please reach out!

Thanks,
Genevieve from Avalara