Skip to content

Commit

Permalink
Add citadel's custom error (#26)
Browse files Browse the repository at this point in the history
feat: add custom errors

Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com>
  • Loading branch information
KeisukeYamashita authored Apr 14, 2023
1 parent f890739 commit e126e00
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/errors/code.ts
Original file line number Diff line number Diff line change
@@ -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",
}
11 changes: 7 additions & 4 deletions src/errors/errors.ts
Original file line number Diff line number Diff line change
@@ -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,
},
});
}
1 change: 1 addition & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./code";
export * from "./errors";

0 comments on commit e126e00

Please sign in to comment.