Videos
Check out our tutorial video series.
Error Handling with Javascript Fetch: A guide on how to handle different response status codes in API design using try-catch keywords and promises with samples.
Response status codes are an important part of API design. They can inform a caller about why a request may have succeeded or failed.
When using Javascript fetch to make HTTP requests an exception is thrown for any response with a code of 400 or more. This will cause a calling function to crash but luckily we can easily handle the situation with Javascripts and
keywords.
You can call any URL using fetch in a browser console.
Notice that the is available on the response type.
The MailSlurp NPM package uses to call the MailSlurp email API. Fetch throws exceptions for HTTP response status codes that are 400 or greater.
(400, 404...) response codes indicate a client error. Access the error message on the response body
(500, 501...) response codes indicate a server error. If encountered please contact support.Any non-2xx response throws an exception. There are valid 404 responses that you should handle, these include the 404 returned from methods when a requested resource can't be found.
Use around MailSlurp methods and use
to access the exception error message and
to access the status code.
You can also handle fetch exceptions using the older Promise then and catch methods.
Check out our tutorial video series.
Email and SMS guides for automation and testing.
View github project code for multiple languages.
Latest posts from the MailSlurp team.
Test, build, and automate messaging with a free MailSlurp account.