From e126e008731646374b0e1dd23a199ccbe4e481d8 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Fri, 14 Apr 2023 20:41:36 +0900 Subject: [PATCH] Add citadel's custom error (#26) feat: add custom errors Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- src/errors/code.ts | 12 ++++++++++++ src/errors/errors.ts | 11 +++++++---- src/errors/index.ts | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 src/errors/code.ts diff --git a/src/errors/code.ts b/src/errors/code.ts new file mode 100644 index 0000000..c846d32 --- /dev/null +++ b/src/errors/code.ts @@ -0,0 +1,12 @@ +/** + * Citadel error codes are custom error codes inspired by Apollo server V2. + * @see {@link https://www.apollographql.com/docs/apollo-server/v2/data/errors/} + * + * The built-in error codes of Apollo V4 have limited expressiveness and can be difficult to handle, so we have defined our own set of error codes in this package. + * @see {@link https://www.apollographql.com/docs/apollo-server/v4/data/errors/} + */ + +export enum CitadelErrorCode { + FORBIDDEN = "FORBIDDEN", + UNAUTHENTICATED = "UNAUTHENTICATED", +} diff --git a/src/errors/errors.ts b/src/errors/errors.ts index a24a56c..50bec63 100644 --- a/src/errors/errors.ts +++ b/src/errors/errors.ts @@ -1,25 +1,28 @@ import { GraphQLError } from "graphql"; +import { CitadelErrorCode } from "./code"; /** - * - * @see {@link https://www.apollographql.com/docs/apollo-server/v2/data/errors/} + * Errors that will be returned if the user are not authenticated resolved by the authentication resolver. */ export function AuthenticationError( message: string = "unauthenticated or the user does not exist" ): GraphQLError { return new GraphQLError(message, { extensions: { - code: "UNAUTHENTICATED", + code: CitadelErrorCode.UNAUTHENTICATED, }, }); } +/** + * Errors returned if the user doesn't have required permissions resolved by the permission resolver. + */ export function ForbiddenError( message: string = "user does not have enough permissions to act this request or the user does not exist" ): GraphQLError { return new GraphQLError(message, { extensions: { - code: "FORBIDDEN", + code: CitadelErrorCode.FORBIDDEN, }, }); } diff --git a/src/errors/index.ts b/src/errors/index.ts index 49bbc16..78c6578 100644 --- a/src/errors/index.ts +++ b/src/errors/index.ts @@ -1 +1,2 @@ +export * from "./code"; export * from "./errors";