Skip to content

Commit

Permalink
fix(bus): fallback to a message if body is stream (#90)
Browse files Browse the repository at this point in the history
fixes #88
  • Loading branch information
derevnjuk authored May 17, 2022
1 parent 80b7785 commit 6dddafa
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 16 deletions.
8 changes: 8 additions & 0 deletions packages/bus/src/exceptions/HttpCommandError.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpCommandError } from './HttpCommandError';
import { AxiosError } from 'axios';
import { Readable } from 'stream';

describe('HttpCommandError', () => {
describe('constructor', () => {
Expand All @@ -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' }
Expand Down
16 changes: 12 additions & 4 deletions packages/bus/src/exceptions/HttpCommandError.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion packages/core/src/exceptions/SecTesterError.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class SecTesterError extends Error {
constructor(message: string) {
constructor(message?: string) {
super(message);
this.name = new.target.name;
}
Expand Down
15 changes: 8 additions & 7 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
29 changes: 29 additions & 0 deletions packages/core/src/utils/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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([
{
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import FormData from 'form-data';
import { Stream } from 'stream';

export const isPresent = <T>(value: T): value is NonNullable<T> =>
value !== null && value !== undefined;
Expand All @@ -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';
3 changes: 1 addition & 2 deletions packages/repeater/src/api/commands/CreateRepeaterRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export class CreateRepeaterRequest extends HttpRequest<CreateRepeaterRequestPayl
super({
payload,
url: '/api/v1/repeaters',
method: 'POST',
expectReply: false
method: 'POST'
});
}
}
3 changes: 2 additions & 1 deletion packages/repeater/src/api/commands/DeleteRepeaterRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export class DeleteRepeaterRequest extends HttpRequest {
super({
url: `/api/v1/repeaters/${payload.repeaterId}`,
method: 'DELETE',
payload: undefined
payload: undefined,
expectReply: false
});
}
}
3 changes: 2 additions & 1 deletion packages/scan/src/commands/StopScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export class StopScan extends HttpRequest {
constructor(id: string) {
super({
url: `/api/v1/scans/${id}/stop`,
payload: undefined
payload: undefined,
expectReply: false
});
}
}

0 comments on commit 6dddafa

Please sign in to comment.