Skip to content

Commit

Permalink
feat(p3): add sinapError
Browse files Browse the repository at this point in the history
  • Loading branch information
pismenskiy authored Apr 29, 2020
1 parent 9d53721 commit 1cbb3e3
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 1 deletion.
69 changes: 69 additions & 0 deletions packages/p3/src/main/ts/sinap/error/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const SinapErrorCodes = {
OK: 0,
SUCCESS: 0,
TIMEOUT: -1,
PROBLEM: -3,
ACCOUNT: -4,
NOT_FOUND: -5,
FORBIDDEN: -7,
REQUEST: -8,
OVERLOAD: -13,
ACCESS: -75,
NOT_COMPLETE: -90,
NOT_IMPLEMENTED: -130,
FORBIDDEN_COUNTRY: -131,
CONFIGURATION: -157,
TOO_LATE: -171,
NOT_ENOUGH: -241,
TOO_MUCH: -242,
REMOTE_DENIAL: -270,
RESPONSE: -271,
COMMUNICATION: -272,
AMOUNT: -275,
OTHER: -300,
DEFAULT_CODE: -300,
SINAP_UNKNOWN: -32090,
SINAP_ILLEGAL_ERROR_PARAMS: -32091,
ERROR_PARAMS_MESSAGE: 'Illegal error params',
} as const

type SinapErrorCodes = typeof SinapErrorCodes

type SinapErrorCode = SinapErrorCodes[keyof SinapErrorCodes]

type SinapError = {
code?: SinapErrorCode
message?: string
data?: string
localizedMessages?: Record<string, any> | string
}

type SinapErrorConstructor = SinapErrorCodes & {
new (
code?: SinapErrorCode,
message?: string,
data?: any,
localizedMessages?: Record<string, any>,
): SinapError
}

// tslint:disable-next-line
export const SinapError: SinapErrorConstructor = Object.assign(class extends Error implements SinapError {
code?: SinapErrorCode
data?: string
localizedMessages?: Record<string, any>

constructor(
code?: SinapErrorCode,
message?: string,
data?: any,
localizedMessages?: Record<string, any>) {
super(message)
Object.setPrototypeOf(this, (this.constructor === Error ? SinapError : this.constructor).prototype)

this.code = code || SinapError.DEFAULT_CODE
this.data = data
this.localizedMessages = localizedMessages || {}
}

}, SinapErrorCodes)
83 changes: 83 additions & 0 deletions packages/p3/src/test/ts/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {SinapError} from '../../main/ts/sinap/error'

describe('SinapError', () => {

describe('constructor', () => {
it('return proper instance', () => {
const error = new SinapError(0, 'foo', 'bar')
expect(error).toBeInstanceOf(SinapError)
expect(error).toBeInstanceOf(Error)
})

it('instance contains the correct field with all args', () => {
const error = new SinapError(-75, 'test', 'data', {foo: 'bar'})
expect(error.code).toBe(-75)
expect(error.message).toBe('test')
expect(error.data).toBe('data')
expect(error.localizedMessages).toMatchObject({foo: 'bar'})
})

it('instance contains the correct field without localizedMessages', () => {
const error = new SinapError(-75, 'test', 'data')
expect(error.code).toBe(-75)
expect(error.message).toBe('test')
expect(error.data).toBe('data')
expect(error.localizedMessages).toMatchObject({})
})

it('instance contains the correct field without localizedMessages and data', () => {
const error = new SinapError(-75, 'test')
expect(error.code).toBe(-75)
expect(error.message).toBe('test')
expect(error.data).toBe(undefined)
expect(error.localizedMessages).toMatchObject({})
})

it('instance contains the correct field without localizedMessages, data , message', () => {
const error = new SinapError(-75)
expect(error.code).toBe(-75)
expect(error.message).toBe('')
expect(error.data).toBe(undefined)
expect(error.localizedMessages).toMatchObject({})
})

it('instance contains the correct field without all args', () => {
const error = new SinapError()
expect(error.code).toBe(-300)
expect(error.message).toBe('')
expect(error.data).toBe(undefined)
expect(error.localizedMessages).toMatchObject({})
})
})

describe('static', () => {
it('class contains correct error codes', () => {
expect(SinapError.OK).toBe(0)
expect(SinapError.SUCCESS).toBe(0)
expect(SinapError.TIMEOUT).toBe(-1)
expect(SinapError.PROBLEM).toBe(-3)
expect(SinapError.ACCOUNT).toBe(-4)
expect(SinapError.NOT_FOUND).toBe(-5)
expect(SinapError.FORBIDDEN).toBe(-7)
expect(SinapError.REQUEST).toBe(-8)
expect(SinapError.OVERLOAD).toBe(-13)
expect(SinapError.ACCESS).toBe(-75)
expect(SinapError.NOT_COMPLETE).toBe(-90)
expect(SinapError.NOT_IMPLEMENTED).toBe(-130)
expect(SinapError.FORBIDDEN_COUNTRY).toBe(-131)
expect(SinapError.CONFIGURATION).toBe(-157)
expect(SinapError.TOO_LATE).toBe(-171)
expect(SinapError.NOT_ENOUGH).toBe(-241)
expect(SinapError.TOO_MUCH).toBe(-242)
expect(SinapError.REMOTE_DENIAL).toBe(-270)
expect(SinapError.RESPONSE).toBe(-271)
expect(SinapError.COMMUNICATION).toBe(-272)
expect(SinapError.AMOUNT).toBe(-275)
expect(SinapError.OTHER).toBe(-300)
expect(SinapError.DEFAULT_CODE).toBe(SinapError.OTHER)
expect(SinapError.SINAP_UNKNOWN).toBe(-32090)
expect(SinapError.SINAP_ILLEGAL_ERROR_PARAMS).toBe(-32091)
expect(SinapError.ERROR_PARAMS_MESSAGE).toBe('Illegal error params')
})
})
})
1 change: 0 additions & 1 deletion packages/p3/src/test/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ describe('P3', () => {
@Client() client: TClient,
@Security() security: TSecurity,
): SinapSuggestResponse {
// @ts-ignore
return [{
foo: 'bar',
id,
Expand Down

0 comments on commit 1cbb3e3

Please sign in to comment.