question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Impossible to handle your errors on my API

See original GitHub issue

Hi! I’m using your module on my API and it’s run perfectly using my oauth2 generated tokens! However, when the token has expired I’m having issues catching the exception.

So this is a problem, because my application crashes due this fail instead of notifying the user their token has expired. How to fix and catch this problem?

This is my try/catch error.

var Gmail = require('node-gmail-api')
var userKey = 'INVALID USER TOKEN HERE'

try {
  var gmail = new Gmail(userKey)
  var s = gmail.messages('label:inbox', { max: 10 })
  s.on('data', function (d) {
    console.log(d.snippet)
  })
}
catch(e) {
    console.log('Your credentials are wrong')
    return 0;
}

So when the token is not valid I have reported this issue:

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: Invalid Credentials
    at Request._callback (/node_modules/node-gmail-api/index.js:84:37)
    at Request.self.callback (/node_modules/request/request.js:186:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (/node_modules/request/request.js:1081:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (/node_modules/request/request.js:1001:12)
    at IncomingMessage.g (events.js:291:16)
    at emitNone (events.js:91:20)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ferreirocommented, Apr 7, 2017

I reply myself to message of Dec 12, 2016 about handling this error. I found a really great stack overflow post that teaches how to handle different errors depending on code implementation and the way new errors are generated.

Best practice for exception handling

So! According to this post (and of course I’ve tried it out), we have to listen for errors before doing any other operation, like the following code (this code is extracted from a real project i’m working on):

fetchAllMessagesFromGmail (url, opts, callback) {
    let messages = []
    var gmail = new Gmail(token)
    var s = gmail.messages('label:inbox', { max: 30 })

    s.on('error', (err) => {
      return callback(err, null)
    })

    s.on('data', function (message) {
      messages.push(message)
    })

    s.on('finish', () => {
      return callback(null, messages)
    })
}
0reactions
nathanbowsercommented, Apr 7, 2017

👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best Practices for REST API Error Handling - Baeldung
The simplest way we handle errors is to respond with an appropriate status code. Here are some common response codes: ... While basic,...
Read more >
Best practices for API error handling and troubleshooting
On server side, Orange APIs handle errors by aborting the request and immediately returning an error response to your application.
Read more >
Best Practices for API Error Handling - Nordic APIs
As we've already said, error codes are extremely useful. Error codes in the response stage of an API is the fundamental way in...
Read more >
Error Handling and Troubleshooting with API - YouTube
An important element of any software application is concise, useful error handling. Error handling refers to the anticipation, detection, ...
Read more >
Graceful error handling in REST-driven web applications
An API client should be able to react accordingly to various types of errors, I would therefore advise that you implement your error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found