Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON.parse Error when sending to a not working host #3

Open
ramioooz opened this issue Apr 30, 2021 · 2 comments · May be fixed by #6
Open

JSON.parse Error when sending to a not working host #3

ramioooz opened this issue Apr 30, 2021 · 2 comments · May be fixed by #6

Comments

@ramioooz
Copy link

ramioooz commented Apr 30, 2021

Hello developer,

I have a postal mail server working just fine
I can send emails using your node js client with no problem.

now I turned my mail server off,
I wanted to see if my application can candle such a scenario.
but now I am getting an error related to your client.
in my case it says something like :

SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at D:\flutterProjects\project_01\node_modules\@atech\postal\Client.js:24:25

in the line 24 to 29 of Client.js
I think instead of:

var json = JSON.parse(content);
if (json.status === 'success') {
    resolve(json.data);
} else {
    reject(json.data);
}

I think you should do something like this:

try {
    var json = JSON.parse(content);
    if (json.status === 'success') {
        resolve(json.data);
    } else {
        reject(json.data);
    }
} catch (error) {
    // reject(error.toString());
    var msg = 'cant parse response. please check your connection to the mail server';
    reject(msg);
}

I hope this helps,
Thank you

@robertlyall
Copy link
Contributor

Raise a PR and I'm sure we'll get it merged!

@mhemrg mhemrg linked a pull request Jul 7, 2023 that will close this issue
@bitbay
Copy link

bitbay commented Sep 24, 2024

I'm not using the client library, but use fetch and still get syntax errors while parsing the response (mine seems to have to do with a failed "mysql" connection), found it in caddy logs

ERROR  POST /api/v1/send/message (500) event=request transaction=REDACTED controller=LegacyAPI::SendController action=message format=*/* method=POST path=/api/v1/send/message request_id=REDACTED ip_address=REDACTED status=500 db_runtime=4.894985198974609 exception_class=Mysql2::Error::ConnectionError exception_message=Lost connection to MySQL server during query exception_backtrace=/usr/local/bundle/gems/mysql2-0.5.6/lib/mysql2/client.rb:151:in `_query'\n/usr/local/bundle/gems/mysql2-0.5.6/lib/mysql2/client.rb:151:in `block in query'\n/usr/local/bundle/gems/mysql2-0.5.6/lib/mysql2/client.rb:150:in `handle_interrupt'\n/usr/local/bundle/gems/mysql2-0.5.6/lib/mysql2/client.rb:150:in `query'

I think the problem is that the postal server has some ambiguous responses under different conditions (maybe sending XML response where JSON is requested by the client?), in which case it shouldn't matter too much where You catch the error.

Responses from the server (2xx-5xx) should result in a valid JSON object, at least if requested with an Accept: application/json header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants