diff --git a/packages/bus/src/exceptions/HttpCommandError.spec.ts b/packages/bus/src/exceptions/HttpCommandError.spec.ts index eedca324..12d547a3 100644 --- a/packages/bus/src/exceptions/HttpCommandError.spec.ts +++ b/packages/bus/src/exceptions/HttpCommandError.spec.ts @@ -1,5 +1,6 @@ import { HttpCommandError } from './HttpCommandError'; import { AxiosError } from 'axios'; +import { Readable } from 'stream'; describe('HttpCommandError', () => { describe('constructor', () => { @@ -12,6 +13,13 @@ describe('HttpCommandError', () => { input: { response: { data: 'Something went wrong', status: 500 } }, expected: { message: 'Something went wrong', status: 500 } }, + { + input: { + response: { data: Readable.from('test'), status: 500 }, + message: 'Something went wrong' + }, + expected: { message: 'Something went wrong', status: 500 } + }, { input: { message: 'Timeout reached', code: 'ETIMEDOUT' }, expected: { message: 'Timeout reached', code: 'ETIMEDOUT' } diff --git a/packages/bus/src/exceptions/HttpCommandError.ts b/packages/bus/src/exceptions/HttpCommandError.ts index 78240dec..4b0ccd3e 100644 --- a/packages/bus/src/exceptions/HttpCommandError.ts +++ b/packages/bus/src/exceptions/HttpCommandError.ts @@ -1,13 +1,21 @@ import { AxiosError } from 'axios'; -import { SecTesterError } from '@sec-tester/core'; +import { SecTesterError, isStream, isPresent } 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; + super(); + const { code, response: { data, status } = {} } = cause; + let { message } = cause; + + if (!isStream(data) && isPresent(data)) { + message = data; + } + + this.message = message; + this.status = status; + this.code = code; } } diff --git a/packages/core/src/exceptions/SecTesterError.ts b/packages/core/src/exceptions/SecTesterError.ts index 85b7caa2..a78dee17 100644 --- a/packages/core/src/exceptions/SecTesterError.ts +++ b/packages/core/src/exceptions/SecTesterError.ts @@ -1,5 +1,5 @@ export class SecTesterError extends Error { - constructor(message: string) { + constructor(message?: string) { super(message); this.name = new.target.name; } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index ba2bca8a..f1f95710 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -7,16 +7,17 @@ export * from './credentials-provider'; export * from './exceptions'; export * from './logger'; export { + NumBoundaries, + checkBoundaries, + contains, delay, - isObject, - isURLSearchParams, isBoolean, - isString, + isDate, isFormData, isNumber, - isDate, + isObject, isPresent, - checkBoundaries, - contains, - NumBoundaries + isStream, + isString, + isURLSearchParams } from './utils'; diff --git a/packages/core/src/utils/types.spec.ts b/packages/core/src/utils/types.spec.ts index 3663f051..474af865 100644 --- a/packages/core/src/utils/types.spec.ts +++ b/packages/core/src/utils/types.spec.ts @@ -5,10 +5,12 @@ import { isNumber, isObject, isPresent, + isStream, isString, isURLSearchParams } from './types'; import FormData from 'form-data'; +import { Readable, Writable } from 'stream'; describe('types', () => { describe('isNumber', () => { @@ -132,6 +134,33 @@ describe('types', () => { }); }); + describe('isStream', () => { + it.each([ + { + input: Readable.from('test'), + expected: true + }, + { + input: new Writable(), + expected: true + }, + { + input: {}, + expected: false + }, + { + input: [], + expected: false + } + ])('should return $expected for $input', ({ input, expected }) => { + // act + const result = isStream(input); + + // arrange + expect(result).toEqual(expected); + }); + }); + describe('isDate', () => { it.each([ { diff --git a/packages/core/src/utils/types.ts b/packages/core/src/utils/types.ts index 8a2abf91..8db63f17 100644 --- a/packages/core/src/utils/types.ts +++ b/packages/core/src/utils/types.ts @@ -1,4 +1,5 @@ import FormData from 'form-data'; +import { Stream } from 'stream'; export const isPresent = (value: T): value is NonNullable => value !== null && value !== undefined; @@ -22,3 +23,8 @@ export const isURLSearchParams = (value: unknown): value is URLSearchParams => export const isFormData = (value: unknown): value is FormData => value instanceof FormData; + +export const isStream = (value: unknown): value is Stream => + isPresent(value) && + typeof value === 'object' && + typeof (value as Stream).pipe === 'function'; diff --git a/packages/repeater/src/api/commands/CreateRepeaterRequest.ts b/packages/repeater/src/api/commands/CreateRepeaterRequest.ts index 056aa193..f2ea483d 100644 --- a/packages/repeater/src/api/commands/CreateRepeaterRequest.ts +++ b/packages/repeater/src/api/commands/CreateRepeaterRequest.ts @@ -10,8 +10,7 @@ export class CreateRepeaterRequest extends HttpRequest