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.

Add error container for API response failures

See original GitHub issue

Given the following example:

commerce.checkout.capture().error(error => this.handleError);

The error object looks like this (in the console):

error
Error: Request failed with status code 422
    at createError (bundle.js:1968)
    at settle (bundle.js:2214)
    at XMLHttpRequest.handleLoad (bundle.js:1481)

The network response is this:

{"error":{"type":"not_valid","message":"Requested quantity not available"},"status_code":422}

In order to get that message, you must use error.response.data.error.message (via Axios). This is a pretty verbose way to get the message from our response, which is a standardised structure from the API.

Perhaps we should wrap these errors in our own container class with easier access to the response data?

cc @chec/engineering

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
ScopeyNZcommented, Dec 3, 2019

I was thinking the code would look like this:

class OurError extends Error {
  constructor(originalError) {
    this.originalError = originalError;
    
    // Extract information from the error response
    super(message);
    this.status_code = 422;
    // Whatever other properties you want to set here...
  }
}

[...].catch(error => {
  throw new OurError(error);
})

End goal is that we have a nicer API for errors from our API, but you can still access the original error if need be.

1reaction
ScopeyNZcommented, Dec 3, 2019

I like the idea of doing a little error handling ourselves. I’m thinking we can create our own error class and make the errors thrown match something like this:

{
  "type": "not_valid",
  "status_code": 422,
  "message": "Requested quantity not available"
  "http_message": "Request failed with status code 422",
}

Should be pretty easy to implement.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API failure reasons - Amazon Elastic Container Service
When an API action that you have triggered through the Amazon ECS API, console, or the AWS CLI exits with a failures error...
Read more >
REST API Error Handling Best Practices
I believe that the best solution to handle errors in a REST API web services is the third option, in short: Use three...
Read more >
How to Fix and Debug Docker Containers Like a Superhero
Container errors are tricky to diagnose, but some investigative magic works wonders. Read along to learn how to debug Docker containers.
Read more >
Error handling in Azure API Management policies
Learn how to respond to error conditions that may occur during the processing of requests in Azure API Management.
Read more >
HTTP status and error codes for JSON | Cloud Storage
The following document provides reference information about the status codes and error messages that are used in the Cloud Storage JSON API.
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