Error Handling With Express Flashcards

1
Q

How can we catch all types of response methods?

A

We can use the all method on the app object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can we match any path in our app?

A

We can use * to match any URL in our app.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Where would we create middleware to handle unhandled routes?

A

We want to create the middleware inside our main file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are operational errors?

A

These are predictable errors that we anticipate and handle in advance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are Programming errors?

A

These are bugs or mistakes introduced during development.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can we implement a global error-handling middleware?

A

We use the app method and pass in the arguments (err, req, res, next).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What do we do inside our global error-handling middleware?

A

Set up properties to handle the status code, defaulting to 500 if not specified, and the status message, defaulting to ‘error’ if absent.

Next, we will send a response with the status code from the error object and a JSON object containing status (set to err.status) and message (set to err.message).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can we call our global error-handling middleware?

A

We invoke next() with our error instance as an argument. Calling next() with an argument triggers the error-handling middleware we previously defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Where should we add the error-handling middleware?

A

You want to create its controller. Export the function, and make a middleware for it in the main file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can we create operational errors?

A

Create a class that extends from the Error class. This custom class constructor accepts a statusCode and message. From the parent Error class, we only inherit the message property.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does isOperational mean?

A

Indicates that any error created with this class is an operational error. This distinction will be useful later when handling different types of errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What folder should the operational error class be in?

A

It should be in the utils folder.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can we refactor handling async errors?

A

We can remove all the catch blocks and create a helper function that will handle the errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Tell me what should the error helper function have?

A

It should accept a function as an argument. This function should return a function with req, res, and next passed in. This returned function should execute the passed-in function, chaining it with a catch method that passes the next method. This next method will automatically receive the error object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can we handle docs not found in our DB due to bad input?

A

We want to check if the document was returned. If the document was not found, we want to return a next function with the error class passed in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why should we create two separate errors for devs and users?

A

For users, we want to generate a friendly and easy-to-understand message. For developers, we aim to provide errors that are more detailed and useful.

17
Q

How can we check if our environment is in production mode or development mode?

A

We use if and else if statements.

18
Q

What should we do if our environment is in production mode?

A

Invoke a function, this function should receive two arguments. The error object and response object. This function checks if our error is isOperational if so, it sends a message, or else it sends a generic message.

19
Q

Why should we check if an error is an operational error during production?

A

This is because some non-operational errors can send error messages that can leak information.

20
Q

How can we handle an invalid database ID?

A

We want to check if the error name is equal to CastError. If it is, we want to call a function named handleCastError.

21
Q

What should we do before working with the error object in our production environment?

A

Create a copy.

22
Q

What will be in the handleCastErr function?

A

In this function, we will pass in a copy of the error object. Inside, we will create a message saying invalid err.path and err.value. This message will be passed into our AppError class, which handles operational errors and marks them as operational errors. We also want to pass in 404 for a bad request status code.

23
Q

How can we handle duplicate errors?

A

We want to check if the error code is equal to 110000. If it is we want to create a function that will return the new error.

24
Q

How can we handle validation errors?

A

We want to check if the error name is equal to ValidationError. If it is, we want to create a function that will return a new error.

25
Q

How can we fetch all the validation errors?

A

To get each error, we use Object.values and pass in err.errors, which will put every object into its own index in an array. We can then loop through this array and return a new array containing each error message, and finally, join them together into a single string.

26
Q

What are unhandled rejections?

A

They are promise rejections that are not handled.

27
Q

How can we catch them?

A

Use the on method found on the process object. Inside, we pass in unhandledRejection and the err object we get from it.

28
Q

What do we do when we have an unhandled rejection?

A

We want to shut down our app.

29
Q

What are uncaught exceptions?

A

They are errors in our synchronous code. Such as calling an undefined variable.

30
Q

How can we handle uncaught exceptions?

A

We need to use the on method on our process object. Inside, we pass in uncaughtException and the error object. We handle this similarly to how we handle unhandled promise rejections.

31
Q

Why do we need to close the app if we have an uncaught exception?

A

We need to close the app because our Node app is in an unclean state.

32
Q

Where do we want to handle our uncaught exceptions?

A

We want to create this process at the very top of our file.