-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(bus): humanize axios error messages (#89)
closes #88
- Loading branch information
Showing
15 changed files
with
129 additions
and
63 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
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
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,30 @@ | ||
import { HttpCommandError } from './HttpCommandError'; | ||
import { AxiosError } from 'axios'; | ||
|
||
describe('HttpCommandError', () => { | ||
describe('constructor', () => { | ||
it.each([ | ||
{ | ||
input: { message: 'Something went wrong' }, | ||
expected: { message: 'Something went wrong' } | ||
}, | ||
{ | ||
input: { response: { data: 'Something went wrong', status: 500 } }, | ||
expected: { message: 'Something went wrong', status: 500 } | ||
}, | ||
{ | ||
input: { message: 'Timeout reached', code: 'ETIMEDOUT' }, | ||
expected: { message: 'Timeout reached', code: 'ETIMEDOUT' } | ||
} | ||
])( | ||
'should create an instance of error if $input is passed', | ||
({ input, expected }) => { | ||
// act | ||
const result = new HttpCommandError(input as AxiosError); | ||
|
||
// assert | ||
expect(result).toMatchObject(expected); | ||
} | ||
); | ||
}); | ||
}); |
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,13 @@ | ||
import { AxiosError } from 'axios'; | ||
import { SecTesterError } from '@sec-tester/core'; | ||
|
||
export class HttpCommandError extends SecTesterError { | ||
public readonly status: number | undefined; | ||
public readonly code: string | undefined; | ||
|
||
constructor(public readonly cause: AxiosError) { | ||
super(cause.response?.data ?? cause.message); | ||
this.status = cause.response?.status; | ||
this.code = cause.code; | ||
} | ||
} |
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 @@ | ||
export * from './HttpCommandError'; |
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
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
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,11 +1,11 @@ | ||
export class EventHandlerNotFound extends Error { | ||
import { SecTesterError } from '../../exceptions'; | ||
|
||
export class EventHandlerNotFound extends SecTesterError { | ||
constructor(...eventNames: string[]) { | ||
super( | ||
`Event handler not found. Please register a handler for the following events: ${eventNames.join( | ||
', ' | ||
)}` | ||
); | ||
this.name = new.target.name; | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
} | ||
} |
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,12 +1,14 @@ | ||
import { CommandDispatcher } from '../CommandDispatcher'; | ||
import { EventDispatcher } from '../EventDispatcher'; | ||
import { SecTesterError } from '../../exceptions'; | ||
import { getTypeName } from '../../utils'; | ||
|
||
export class IllegalOperation extends Error { | ||
export class IllegalOperation extends SecTesterError { | ||
constructor(instance: EventDispatcher | CommandDispatcher) { | ||
super( | ||
`Please make sure that ${instance.constructor.name} established a connection with host.` | ||
`Please make sure that ${getTypeName( | ||
instance | ||
)} established a connection with host.` | ||
); | ||
this.name = new.target.name; | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
} | ||
} |
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,7 +1,7 @@ | ||
export class NoResponse extends Error { | ||
import { SecTesterError } from '../../exceptions'; | ||
|
||
export class NoResponse extends SecTesterError { | ||
constructor(duration: number) { | ||
super(`No response for ${duration} seconds.`); | ||
this.name = new.target.name; | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
} | ||
} |
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,11 +1,13 @@ | ||
import { EventHandler } from '../EventHandler'; | ||
import { SecTesterError } from '../../exceptions'; | ||
import { getTypeName } from '../../utils'; | ||
|
||
export class NoSubscriptionsFound extends Error { | ||
export class NoSubscriptionsFound extends SecTesterError { | ||
constructor(handler: EventHandler<unknown, unknown>) { | ||
super( | ||
`No subscriptions found. Please use '@bind()' decorator to subscribe ${handler.constructor.name} to events.` | ||
`No subscriptions found. Please use '@bind()' decorator to subscribe ${getTypeName( | ||
handler | ||
)} to events.` | ||
); | ||
this.name = new.target.name; | ||
Object.setPrototypeOf(this, new.target.prototype); | ||
} | ||
} |
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,5 +1,8 @@ | ||
export class UnsupportedEventType extends Error { | ||
import { SecTesterError } from '../../exceptions'; | ||
import { getTypeName } from '../../utils'; | ||
|
||
export class UnsupportedEventType extends SecTesterError { | ||
constructor(event: unknown) { | ||
super(`${typeof event} cannot be used with the @bind decorator.`); | ||
super(`${getTypeName(event)} cannot be used with the @bind decorator.`); | ||
} | ||
} |
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,6 @@ | ||
export class SecTesterError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = new.target.name; | ||
} | ||
} |
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 @@ | ||
export * from './SecTesterError'; |
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