postal-node icon indicating copy to clipboard operation
postal-node copied to clipboard

JSON.parse Error when sending to a not working host

Open ramioooz opened this issue 3 years ago • 1 comments

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

ramioooz avatar Apr 30 '21 08:04 ramioooz

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

robertlyall avatar Dec 27 '21 20:12 robertlyall