You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Error handling is important so we have visibility of issues that occur in applications, and gain some understanding of what is going wrong.
47
+
48
+
### HTTP Status Codes Refresher
49
+
50
+
Here are some of the most commonly used:
51
+
52
+
#### 2XX - Success
53
+
`200 OK` - The request succeeded, e.g. a webpage loads as it should.
54
+
`201 Created` - A new resource was made, e.g. a new user account.
55
+
56
+
#### 3XX - Redirection
57
+
`301 Moved Permanently` - The URL has changed, e.g. redirect from oldsite.com to newsite.com.
58
+
`302 Found` - A temporary redirect, e.g. redirecting Spanish visitors to the Spanish version of the website.
59
+
60
+
#### 4XX - Client Errors
61
+
`400 Bad Request` - The request was invalid, e.g. form data missing or incorrect.
62
+
`401 Unauthorized` - You need to log in e.g. trying to access user features when logged out.
63
+
`404 Not Found` - Nothing at that URL e.g. a missing page or resource.
64
+
65
+
#### 5XX - Client Errors
66
+
`500 Internal Server Error` - Generic server issue, e.g. something goes wrong in the backend.
67
+
`503 Service Unavailable` - Server is down or busy e.g. backend API is not running.
68
+
69
+
Read more at [HTTP Status cheatsheet](https://devhints.io/http-status).
70
+
71
+
### Client vs Server
72
+
73
+
Server-side errors should be designed for developers. Detailed errors help debugging and ultimately fixing issues easier.
74
+
e.g. If a database table is missing, record the missing table name in your logs.
75
+
76
+
Client-side errors should be designed for users, including the correct HTTP status code.
77
+
e.g. In the missing database table case, simply return a `500 Internal Server Error` and a useful page to the user to explain how to continue.
78
+
79
+
It's important to hide specific error details from the user for multiple reasons:
80
+
1. Security - Revealing database names and other internal details can give attackers too many clues about your system which can make your app more vulnerable to exploitation.
81
+
2. Privacy - Many internal errors can include sensitive data (e.g. user IDs, personal information) that shouldn't be exposed.
82
+
3. User Experience - Some technical errors would confuse most users, so stick with simple, friendly messages that can help the user continue.
83
+
84
+
### Live coding
85
+
86
+
Walk through [`api/contacts.js`](./session-materials/phonebook/api/contacts.js) to explain the try/catch pattern, appropriate server and client side error handling, correct usage of HTTP codes and why the knex code is insecure.
87
+
44
88
## Advanced Postman
45
89
46
90
Postman can be used for quickly testing APIs, but it can also be configured in more advanced ways to support the development workflow. Here are four ways trainees can level up their Postman game.
0 commit comments