Skip to content

Commit

Permalink
feat: add unauthorized error utilities (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcogabbo committed May 8, 2024
1 parent 993f47d commit 8589c5b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/__tests__/responses.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
HttpStatusCodeEnum,
ResponseErrorPreconditionFailed,
ResponseErrorUnauthorized,
ResponseSuccessJson,
} from "../responses";

Expand Down Expand Up @@ -35,6 +36,18 @@ describe("ResponseSuccessJson", () => {
});
});

describe("ResponseErrorUnauthorized", () => {
const anErrorDetail = "Invalid credentials";
it("should return the standard response", () => {
expect(ResponseErrorUnauthorized(anErrorDetail)).toMatchObject(
expect.objectContaining({
kind: "IResponseErrorUnauthorized",
detail: `Unauthorized: ${anErrorDetail}`,
})
);
});
});

describe("ResponseErrorForbiddenNotAuthorized", () => {
it("should return the standard response", () => {
expect(getResponseErrorForbiddenNotAuthorized()).toMatchObject(
Expand Down
21 changes: 21 additions & 0 deletions src/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,27 @@ export const ResponseErrorFromValidationErrors =
return ResponseErrorValidation(`Invalid ${type.name}`, detail);
};

/**
* Interface for 401 unauthorized
*/
export type IResponseErrorUnauthorized =
IResponse<"IResponseErrorUnauthorized"> & { readonly detail: string };

/**
* Returns an unauthorized error response with status code 401.
*/
export const ResponseErrorUnauthorized = (
detail: string
): IResponseErrorUnauthorized => ({
...ResponseErrorGeneric(
HttpStatusCodeEnum.HTTP_STATUS_401,
"Unauthorized",
detail
),
detail: `Unauthorized: ${detail}`,
kind: "IResponseErrorUnauthorized",
});

/**
* The user is not allowed here.
*/
Expand Down

0 comments on commit 8589c5b

Please sign in to comment.