06 - RESPONSE NORMALIZATION STRATEGIES Flashcards

1
Q

creating a currentUser route handler

A

export { router as currentUserrouter }
app.use(currrentUserRouter);

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

validating requests with express-validator

A

import body, validationResult

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

how to handle and structure errors

A

consistent structure for validation and other type of errors in a middleware

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

error handler

A

import { Request, Response, NextFunction } from “response”;

export const errorHandler = (
err: Error,
req: Request,
res: Response,
next: NextFunction
) => {

};

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

creating a RequestValidationError class

A

import { ValidationError } from “express-validator”;

export class RequestValidationError extends Error {
constructor(public errors: ValidationError[ ] ) {
super( );

  // only when extending a build in class
  Object.setPrototypeOf(this, RequestValidationError.prototype);    } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

database connection error class

A

export class DatabaseConnectionError extends Error {
reason = “Error connecting to database”;

constructor( ) {
  super( );
   
  // only when extending a build in class
  Object.setPrototypeOf(this, DatabaseConnectionError.prototype);    } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

throwing custom error classes

A

throw new RequestValidationError(errors.array( ));
throw new DatabaseConnectionError( );

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

common response error structure

A

{
errors: {
message: string, field?: string
}[ ]
}

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

add serializeErrors method to each error class

A

serializeErrors( ) {
return [
{ message: “reason” }
];
}
}

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

serialize method in RequestValidationError class

A

serializeErrors( ) {
return this.errors.map(err => {
return { message: err.msg, field: err.param }
});
}

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

create CustomError Abstract class

A

export abstract class CustomError extends Error {
abstract statusCode: number;

constructor( ) {
  super( );
   
  Object.setPrototypeOf(this, CustomError.prototype);    }

abstract serializeErrors( ): { message: string; field?: string } [ ] }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

using CustomError in errorHandler middleware

A

if (err instanceOf CustomError) {
return status(err.statusCode).send({ errors: err.serializeErrors( ) });
}

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

create a route not found custom error

A

import { CustomError } from “custom-error”;

export class NotFoundError extends CustomError {
statusCode = 404;

constructor( ) {
  super("Route not found");
   
  Object.setPrototypeOf(this, NotFoundError.prototype);    }

 serializeErrors( ) {
     return [ { message: "Not Found" } ]
 } }

app.get(“*”, ( ) => {
throw new NotFoundError( )
});

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

install express-async-errors

A

app.all(“*”, async (req, res, next ) => {
next ( new NotFoundError( ));
});

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