Max S
Max S

Reputation: 119

Handling errors in browser script

There is a simple code excerpt:

gapi.client.sheets.spreadsheets.values.get({
    spreadsheetId,
    range: "TOP!A:B"
}).then(result => {
       doSomething()
})

How to detect and handle errors if they occur?

Errors like 503,403 etc. that are appear in response data:

{error:...}

Upvotes: 2

Views: 67

Answers (1)

Tanaike
Tanaike

Reputation: 201553

For example, how about this modification?

Modified script:

gapi.client.sheets.spreadsheets.values.get({
    spreadsheetId,
    range: "TOP!A:B"
}).then(result => {
       doSomething()
}, (error) => { // Added
    if (error.status == 403) { // You can also use switch().
      console.log('Status code is 403. Message: %s', error.result.error.message);
    } else if (error.status == 503) {
      console.log('Status code is 503. Message: %s', error.result.error.message);
    }
})

Reference:

If this was not what you want, I'm sorry.

Upvotes: 1

Related Questions