Skip to content

Commit

Permalink
feat: literal error code type
Browse files Browse the repository at this point in the history
  • Loading branch information
star0202 committed Jun 24, 2023
1 parent 53a19b5 commit 6eb5e1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ export class LimitUseAuthenticationKeyError extends HTTPError {}

export class DataNotFoundError extends HTTPError {}

export type ErrorCode =
| 'ERROR-300'
| 'ERROR-290'
| 'ERROR-310'
| 'ERROR-333'
| 'ERROR-336'
| 'ERROR-337'
| 'ERROR-500'
| 'ERROR-600'
| 'ERROR-601'
| 'INFO-300'
| 'INFO-200'

export const ErrorsMapping = {
'ERROR-300': MissingRequiredValueError,
'ERROR-290': AuthenticationError,
Expand All @@ -48,4 +61,4 @@ export const ErrorsMapping = {
'ERROR-601': SQLStatementError,
'INFO-300': LimitUseAuthenticationKeyError,
'INFO-200': DataNotFoundError,
} satisfies Record<string, typeof HTTPError>
} satisfies Record<ErrorCode, typeof HTTPError>
4 changes: 2 additions & 2 deletions src/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorsMapping, RequestTimeoutError } from './errors'
import { ErrorCode, ErrorsMapping, RequestTimeoutError } from './errors'
import type {
AcaInsTiInfoParam,
AcaInsTiInfoResponse,
Expand Down Expand Up @@ -180,7 +180,7 @@ export class NeisRequest {
})

if (data.RESULT) {
const code = data.RESULT.CODE as keyof typeof ErrorsMapping
const code = data.RESULT.CODE as ErrorCode
const err = new ErrorsMapping[code](code, data.RESULT.MESSAGE)

this.logger?.error(err)
Expand Down

0 comments on commit 6eb5e1d

Please sign in to comment.