Skip to content

JSON.parse error #11

@titulus

Description

@titulus

When odoo server is not working propperly, it can respond anything instaead of data you want.
Like

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Instead of json when postgres is down.

Example of code:

try {
  odoo.connect(function (err) {
    if (err) { return console.log('odoo.connect handle error: ',err); }
  });
} catch (err) {
  console.log('odoo.connect error: ',err);
}

Error throwing:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
^

SyntaxError: Unexpected token <
    at Object.parse (native)
    at IncomingMessage.<anonymous> (D:\work\a.kuntsevich\Dropbox\work\vnc-project\vnc-proj\service\node_modules\odoo\lib\index.js:53:23)
    at emitNone (events.js:72:20)
    at IncomingMessage.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:905:12)
    at nextTickCallbackWith2Args (node.js:474:9)
    at process._tickCallback (node.js:388:17)
[Finished in 0.7s with exit code 1]

It can't be handled with try{} catch{} construction too

But wrapping response = JSON.parse(response); into try{} catch{} can solve this problem:

try {
  response = JSON.parse(response);
} catch (err) {
  return cb(err, null);
}

in res.on('end', ...) will handle the error instead of throwing it:

odoo.connect handle error:  [SyntaxError: Unexpected token <]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions