Giter VIP home page Giter VIP logo

Comments (9)

jerbob92 avatar jerbob92 commented on June 9, 2024

I'll check for Windows support, shouldn't be too hard.

About the JSON Parse, sending an empty string to JSON.parse should not crash your app, it should just result in an error "JS: ERROR SyntaxError: Unexpected end of JSON input".

Are you sure there wasn't something else that caused this? I think console.log() triggers a toString() on the content and that is causing some issues. I'll try to see if I can reproduce it.

from nativescript-http.

jerbob92 avatar jerbob92 commented on June 9, 2024

Ok, I found out the crash:
console.log calls toString on the result, the toString method on the result is this:

                let str: string;
                if (encoding) {
                    str = decodeResponse(result.raw, encoding);
                } else if (result.responseAsString) {
                    str = result.responseAsString;
                }

                if (typeof str === "string") {
                    return str;
                } else {
                    throw new Error("Response content may not be converted to string");
                }

Which is the same as in core HTTP. When the result is not a string, it will throw the error, and console.log does not like it. So it's a bug in console.log implementation of NS. I will open a bug in core.

from nativescript-http.

manciuszz avatar manciuszz commented on June 9, 2024

I'll check for Windows support, shouldn't be too hard.

a simple one liner result.context = result.context.replace(/\\/g, '/'); is enough to fix the issue.

About the JSON Parse, sending an empty string to JSON.parse should not crash your app, it should just result in an error "JS: ERROR SyntaxError: Unexpected end of JSON input".

That's exactly the error I got, which triggered a '.catch' on a promise when sending a http request which at that point in time still had some broken code that lead to an app crash.

Are you sure there wasn't something else that caused this? I think console.log() triggers a toString() on the content and that is causing some issues. I'll try to see if I can reproduce it.

Yea, like I thought - it is a bug with 'console.log'. Anyways, a quick try/catch for the win! xD

from nativescript-http.

jerbob92 avatar jerbob92 commented on June 9, 2024

Is it possible that you were trying to get the result as json? Because a console.log would not trigger that.

However, I did find a bug in console.log in NS, when you have an object that implements toString(), and you console.log that object, and the toString throws an error, the whole app crashes, so maybe something like that happened to you? I will open a bug report soon.

For the Windows paths you can probably just use path.sep from nodejs as path separator instead of hardcoded slash. I will add that soon.

from nativescript-http.

manciuszz avatar manciuszz commented on June 9, 2024

Is it possible that you were trying to get the result as json? Because a console.log would not trigger that.

deleteRequest: (resource, params = {}) =>
            httpClient(resource + generateParamQuery(params.query, "?"), {
                ...params,
                method: "DELETE",
            }).then((response) => {
                const result = response;
                try { console.log(TAG, "[fetchDelete-Response]", resource, result); } catch {} // NOTE: This line is haunted. For whatever magic reason this line calls .toJSON() when stringifying the output and the JSON input happens to be an empty string.
                return result.ok ? result : Promise.reject(result);
            }).catch((e) => {
                console.log(TAG, "[fetchDelete-Error]", resource + generateParamQuery(params.query, "?"), e);
                throw(e);
            }
        ),

 // ... after the 'deleteRequest' was sent, the returned 'response' is an object that contains a function 'json: () => Promise.resolve(response.content.toJSON())'

Here's a debugger screenshot without try/catch:
chrome_xZiKiFBUy7

chrome_FuCIYNyel2

Spooky.

from nativescript-http.

jerbob92 avatar jerbob92 commented on June 9, 2024

Can you tell me which http client you are using? I don't really think this is something we should solve in this plugin.

from nativescript-http.

manciuszz avatar manciuszz commented on June 9, 2024

httpClient is basically just a 'fetch' wrapper that uses 'nativescript-http' Http.request method.

import { Http } from "@nativescript/core";

export const fetch = (url, options = {}) => { // fetch adapter for 'Http.request'
    if (url instanceof Request) {
        options = (({ mode, headers, method, _bodyText }) => ({ mode, method, headers: headers.map, body: _bodyText }))(url);
        url = url.url;
    }

    options.url = url;
    options.timeout = options.timeout || 3000;
    options.content = options.content || options.body;
    return Http.request(options).then((response) => {
        response.status = response.statusCode;
        response.ok = response.statusCode >= 200 && response.statusCode < 300;
        response.json = () => Promise.resolve(response.content.toJSON());
        return response;
    });
};

Anyways, this 'console.log' issue is just something I randomly encountered after installing 'nativescript-http' plugin. It's baffling, sure, but quite easy to handle.

from nativescript-http.

jerbob92 avatar jerbob92 commented on June 9, 2024

I have just done a test. When you console.log response, it does not execute the json method, and thus also does not result in the given error.

from nativescript-http.

jerbob92 avatar jerbob92 commented on June 9, 2024

Fixed with #52

from nativescript-http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.