-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom errors Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com>
- Loading branch information
1 parent
f890739
commit e126e00
Showing
3 changed files
with
20 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./code"; | ||
export * from "./errors"; |