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.

Handle not found from API call on server-side

See original GitHub issue

Hi,

I’m trying to figure out how to return 404 server-side when the id cannot be found in the API. The route is looking something like this /:id. I know this example app does not cover that case but maybe all you smart people here have some pointers? Given the solution I’ll be happy to add it here.

Cheers, K

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
truetercommented, Nov 11, 2015

When your application requests a record, the JSON API responds with an error code like 404, and possibly additional information like a “Record not found”-message. It is up to the client to implement how this information is displayed to the user. Setup a route like /users/:id in the routes file on the server and have the linked component deal with the scenarios of both the record being available and not being available.

1reaction
truetercommented, Nov 11, 2015

Are you referring to a route that can’t be mapped to an action? That defaults to 404 here. If you want to return a 404 from an api action you can pass a status like this:

export default function action( req ) {
    return new Promise( ( resolve, reject ) => { 
        getRecord( id, ( err, data ) => {
            if( err )
                return reject( err )
            if( ! data )
                return reject({ status : 404 })

            resolve( data )
        })
    })
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling Exceptions Returned from the Web API
You'll learn to determine what kind of error is returned from your Web API call and display different messages based on those errors....
Read more >
Handle Resource not found in Rest API - Stack Overflow
The server has not found anything matching the requested address (URI) ( not found ). This means the URL you have typed is...
Read more >
Best Practices for REST API Error Handling - Baeldung
Learn about some of the best practices for handling REST API errors, including useful approaches for providing users with additional ...
Read more >
10 Error Status Codes When Building APIs For The First Time ...
Usually, an HTTP request with an inappropriate method simply results in a 404 not found status. A not-implemented status implies that the method ......
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 >

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