2

I'm working on a React Native application, which fetches an API response from a server. Sometimes, it returns success, but sometimes it catches an error.

The following code is my fetch function:

export default async (url, body = null, method = 'GET') => {
    let config = {
        method,
    };
    return await fetch(url, config).then((response) => {
        if (!response.ok) {
            throw Error(response.statusText);
        }
        return response.json();
    }).catch(error => {
        console.warn(error); // sometimes it was catched in here
    });
};

It was returning various error messages, e.g:

[SyntaxError: JSON Parse error: "\u302\" is not a valid unicode escape]
[SyntaxError: JSON Parse error: Invalid escape character 4]
[SyntaxError: JSON Parse error: "\u740\" is not a valid unicode escape]

I'm already checked my API response through the browser and it didn't find anything wrong with the response. I thought there was something wrong with my ES code maybe.

What is going wrong here...?

1 Answer 1

0

The \u302\ character appears to be something similar to the ^ symbol called a "COMBINING CIRCUMFLEX ACCENT" http://www.fileformat.info/info/unicode/char/0302/index.htm

And \u740\ is a "SYRIAC FEMININE DOT http://www.fileformat.info/info/unicode/char/0740/index.htm

The JSON parser just isn't able to handle these characters I guess, or at least not in the way they're presented. It looks like they're being used as an escape character, but it's hard to know since you didn't provide samples of the output that generated each error.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the answer. Here's sample JSON output: pastebin.com/vduDQJ8g. But, sometimes it can handle it.
Which error maps to this output? If you could provide the entire error message that couples to this JSON output, that would help.
It's hard to find where is exactly error location because sometimes it was parsed successfully. Is it a bug in fetch?
I think it's an issue with the data. So when an error occurs, capture and analyze the response along with the specific error to figure this out.
Is there any issue with async process? Perhaps the response is not completed yet, but the JSON parse process is already running so it throwed an error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.