Status Codes

The Codes You Will Use Most

CodeNameWhen ShopEase returns it
200OKGET, PUT, PATCH succeeded
201CreatedPOST created a product (NestJS default for @Post)
204No ContentDELETE succeeded, nothing to return
400Bad RequestValidation failed (price is negative)
401UnauthorizedNo or invalid login token
403ForbiddenLogged in but not allowed (customer tries admin route)
404Not FoundProduct id does not exist
409ConflictEmail already registered
429Too Many RequestsRate limit hit
500Internal Server ErrorA bug or crash on the server

Status Codes as Code

Example

NestJS has an HttpStatus enum so you never type magic numbers. Here is a tiny version of the same idea, plus a helper that tells you the class of a code:

enum HttpStatus {
  OK = 200,
  CREATED = 201,
  NO_CONTENT = 204,
  BAD_REQUEST = 400,
  NOT_FOUND = 404,
  INTERNAL_SERVER_ERROR = 500,
}

function kind(code: number): string {
  if (code >= 500) return 'server error';
  if (code >= 400) return 'client error';
  if (code >= 300) return 'redirect';
  if (code >= 200) return 'success';
  return 'informational';
}

for (const code of [HttpStatus.CREATED, HttpStatus.NO_CONTENT, HttpStatus.NOT_FOUND, HttpStatus.INTERNAL_SERVER_ERROR]) {
  console.log(code, HttpStatus[code], '->', kind(code));
}

Output

201 CREATED -> success
204 NO_CONTENT -> success
404 NOT_FOUND -> client error
500 INTERNAL_SERVER_ERROR -> server error