Skip to content
This repository was archived by the owner on Sep 4, 2024. It is now read-only.

Commit 58af4c1

Browse files
Refactored responses
1 parent e1f95c1 commit 58af4c1

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

lib/errors.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
import ApiResponse from "./api-response";
1+
import { Response } from "express";
22

3-
export function ApplicationError(code: number, type: string, message: string): ApiResponse {
4-
return {
3+
export function ApplicationError(res: Response, code: number, type: string, message: string) {
4+
return res.status(code).json({
55
success: false,
66
error: {
77
code,
88
type,
99
message
1010
}
11-
}
11+
});
1212
}
1313

14-
export function InvalidFormDataError(message: string = 'Invalid form data!') {
15-
return ApplicationError(400, 'InvalidFormData', message);
14+
export function InvalidFormDataError(res: Response, message: string = 'Invalid form data!') {
15+
return ApplicationError(res, 400, 'InvalidFormData', message);
1616
}
1717

18-
export function AuthenticationError(message: string = 'User is not Authenticated!') {
19-
return ApplicationError(401, 'AuthenticationError', message);
18+
export function AuthenticationError(res: Response, message: string = 'User is not Authenticated!') {
19+
return ApplicationError(res, 401, 'AuthenticationError', message);
2020
}
2121

22-
export function ForbiddenError(message: string = 'User is forbidden from accessing this resource!') {
23-
return ApplicationError(403, 'ForbiddenError', message);
22+
export function ForbiddenError(res: Response, message: string = 'User is forbidden from accessing this resource!') {
23+
return ApplicationError(res, 403, 'ForbiddenError', message);
2424
}
2525

26-
export function NotFoundError(message: string = 'The resource you were looking for was not found on this server.') {
27-
return ApplicationError(404, 'NotFoundError', message);
26+
export function NotFoundError(res: Response, message: string = 'The resource you were looking for was not found on this server.') {
27+
return ApplicationError(res, 404, 'NotFoundError', message);
2828
}
2929

30-
export function ConflictError(message: string = 'The resource you are trying to create already exists!') {
31-
return ApplicationError(409, 'Conflict', message);
30+
export function ConflictError(res: Response, message: string = 'The resource you are trying to create already exists!') {
31+
return ApplicationError(res, 409, 'Conflict', message);
3232
}
3333

34-
export function ServerError(message: string = 'Server Error!') {
35-
return ApplicationError(500, 'ServerError', message);
34+
export function ServerError(res: Response, message: string = 'Server Error!') {
35+
return ApplicationError(res, 500, 'ServerError', message);
3636
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Response } from "express";
22

3-
export default function generateSuccessResponse<T>(res: Response, payload: T, status: number = 200) {
3+
export default function SuccessResponse<T>(res: Response, payload: T, status: number = 200) {
44
return res.status(status).json({
55
success: true,
66
payload: payload

0 commit comments

Comments
 (0)