Skip to main content

Status Code

Status code is a way to represent the result of a request. These HTTP responses indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:

  • Informational responses (100-199)
  • Successful responses (200-299)
  • Redirection messages (300-399)
  • Client error responses (400-499)
  • Server error responses (500-599)

You can find more details about the status code in the MDN Web Docs.

tip

The status codes listed below are defined by RFC 9110

ExpressoTS status code

In ExpressoTS, we have a list of all status codes listed in the MDN that you can use in your application. Here is an example of use:

In a Controller

import { StatusCode } from "@expressots/core";

@controller("/user/create")
class CreateUserController extends BaseController {
constructor(private createUserUseCase: CreateUserUseCase) {
super("create-user-controller");
}

@httpPost("/")
execute(
@requestBody() data: ICreateUserRequestDTO,
@response() res: any
): ICreateUserResponseDTO {
return this.callUseCase(
this.createUserUseCase.execute(data),
res,
StatusCode.Created // Status code
);
}
}

In a Use Case

import { StatusCode } from "@expressots/core";

@provide(CreateUserUseCase)
class CreateUserUseCase {
constructor(private userRepository: UserRepository) {}

execute(data: ICreateUserRequestDTO): ICreateUserResponseDTO | null {
try {
const { name, email } = data;

const user: User | null = this.userRepository.create(new User(name, email));

if (!user) {
Report.Error("User already exists", StatusCode.BadRequest, "create-user-usecase");
}

let response: ICreateUserResponseDTO;

if (user !== null) {
response = {
id: user.Id,
name: user.name,
email: user.email,
status: "success",
};
return response;
}

return null;
} catch (error: any) {
throw error;
}
}
}

Support the project

ExpressoTS is an MIT-licensed open source project. It's an independent project with ongoing development made possible thanks to your support. If you'd like to help, please consider: