Skip to content

Commit

Permalink
feat: errors
Browse files Browse the repository at this point in the history
  • Loading branch information
star0202 committed Apr 29, 2023
1 parent 4067d2e commit 552b0cf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class NeisError extends Error {}

class HTTPError extends NeisError {
constructor(code: string, msg: string) {
super(`${code}: ${msg}`)
this.name = this.constructor.name
}
}

export class MissingRequiredValueError extends HTTPError {}

export class AuthenticationError extends HTTPError {}

export class ServiceNotFoundError extends HTTPError {}

export class InvalidLocationValueError extends HTTPError {}

export class RequestLimitExceededError extends HTTPError {}

export class DailyTrafficLimitExceededError extends HTTPError {}

export class ServerError extends HTTPError {}

export class DatabaseConnectionError extends HTTPError {}

export class SQLStatementError extends HTTPError {}

export class LimitUseAuthenticationKeyError extends HTTPError {}

export class DataNotFoundError extends HTTPError {}

export const ErrorsMapping: {
[key: string]: new (code: string, msg: string) => NeisError
} = {
'ERROR-300': MissingRequiredValueError,
'ERROR-290': AuthenticationError,
'ERROR-310': ServiceNotFoundError,
'ERROR-333': InvalidLocationValueError,
'ERROR-336': RequestLimitExceededError,
'ERROR-337': DailyTrafficLimitExceededError,
'ERROR-500': ServerError,
'ERROR-600': DatabaseConnectionError,
'ERROR-601': SQLStatementError,
'INFO-300': LimitUseAuthenticationKeyError,
'INFO-200': DataNotFoundError,
}
8 changes: 6 additions & 2 deletions src/http.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ErrorsMapping } from './errors'
import type {
AcaInsTiInfoParam,
AcaInsTiInfoResponse,
Expand Down Expand Up @@ -134,9 +135,12 @@ export class NeisRequest {
const { data } = await this.rest.request(config)

if (data.RESULT) {
this.logger?.error(`${data.RESULT.CODE} ${data.RESULT.MESSAGE}`)
const code = data.RESULT.CODE
const err = new ErrorsMapping[code](code, data.RESULT.MESSAGE)

throw new Error(`${data.RESULT.CODE} ${data.RESULT.MESSAGE}`)
this.logger?.error(err)

throw err
}

return Object.values((Object.values(data) as object[][])[0][1])[0] as T[]
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './client'
export * from './errors'
export * from './types'

0 comments on commit 552b0cf

Please sign in to comment.