Skip to content

Commit

Permalink
feat: exception package (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyesungoh authored Jun 19, 2024
1 parent ab1365b commit f2023ee
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"@gitanimals/ui": "workspace:*",
"@gitanimals/exceptions": "workspace:*",
"@tanstack/react-query": "^5.32.0",
"axios": "^1.6.8",
"framer-motion": "^11.1.7",
Expand Down
10 changes: 10 additions & 0 deletions packages/exceptions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@gitanimals/exceptions",
"version": "0.0.0",
"main": "./src/index.ts",
"devDependencies": {
"@gitanimals/eslint-config": "workspace:*",
"@gitanimals/typescript-config": "workspace:*",
"typescript": "*"
}
}
15 changes: 15 additions & 0 deletions packages/exceptions/src/ApiException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// TODO: 에러 스키마 논의 필요
interface ErrorSchema {
code: number;
message: string;
}

export class ApiException extends Error {
declare code: number;

constructor(data: ErrorSchema) {
super(data.message);
this.code = data.code;
this.name = 'ApiException';
}
}
19 changes: 19 additions & 0 deletions packages/exceptions/src/CustomException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type CustomErrorCode = 'NETWORK_ERROR' | 'NETWORK_TIMEOUT' | 'UNKNOWN_ERROR';

const ERROR_MESSAGE: Record<CustomErrorCode, string> = {
NETWORK_ERROR: 'Network error',
NETWORK_TIMEOUT: 'Network timeout',
UNKNOWN_ERROR: 'Unknown error',
};

export class CustomException extends Error {
declare code: CustomErrorCode;

constructor(code: CustomErrorCode, message?: string) {
const errorMessage = message || ERROR_MESSAGE[code];

super(errorMessage);
this.name = 'CustomException';
this.code = code;
}
}
2 changes: 2 additions & 0 deletions packages/exceptions/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './ApiException';
export * from './CustomException';
5 changes: 5 additions & 0 deletions packages/exceptions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "@gitanimals/typescript-config/base.json",
"include": ["src"],
"exclude": ["node_modules"]
}
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f2023ee

Please sign in to comment.