Skip to content

Commit

Permalink
N21-1456 changes errors 2
Browse files Browse the repository at this point in the history
  • Loading branch information
arnegns committed Nov 9, 2023
1 parent 8f4c8bc commit 39164c6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 39 deletions.
33 changes: 9 additions & 24 deletions apps/server/src/core/error/loggable/axios-error.loggable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,28 @@
import { axiosErrorFactory } from '@shared/testing/factory/axios-error.factory';
import { AxiosError } from 'axios';
import { AxiosErrorLoggable } from './axios-error.loggable';

describe(AxiosErrorLoggable.name, () => {
describe('getLogMessage', () => {
const setup = () => {
const axiosError = {
response: {
data: {
error: {
message: 'mockMessage',
},
},
},
status: 400,
stack: 'mockStack',
} as unknown as AxiosError;
const error = new Error('some error message');
const type = 'mockType';
const axiosError: AxiosError = axiosErrorFactory.withError(error).build();

const axiosErrorLoggable = new AxiosErrorLoggable(axiosError, type);
return { axiosErrorLoggable };

return { axiosErrorLoggable, error, axiosError };
};

it('should return error log message', () => {
const { axiosErrorLoggable } = setup();
const { axiosErrorLoggable, error, axiosError } = setup();

const result = axiosErrorLoggable.getLogMessage();

expect(result).toEqual({
type: 'mockType',
error: {
response: {
data: {
error: {
message: 'mockMessage',
},
},
},
status: 400,
stack: 'mockStack',
},
message: axiosError.message,
data: error,
stack: 'mockStack',
});
});
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/core/error/loggable/axios-error.loggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { AxiosError } from 'axios';

export class AxiosErrorLoggable extends HttpException implements Loggable {
constructor(private readonly axiosError: AxiosError, protected readonly type: string) {
super(JSON.stringify(axiosError.response?.data), axiosError.status ?? 500, { cause: axiosError.cause });
super(axiosError.response?.data as string, axiosError.status ?? 500, { cause: axiosError.cause });
}

getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage {
return {
message: this.axiosError.message,
type: this.type,
error: this.axiosError.response?.data as Error,
data: this.axiosError.response?.data as string,
stack: this.axiosError.stack,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe(HydraOauthFailedLoggableException.name, () => {
type: 'HYDRA_OAUTH_FAILED',
message: axiosError.message,
stack: axiosError.stack,
error,
data: error,
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe(TokenRequestLoggableException.name, () => {
expect(logMessage).toStrictEqual({
type: 'OAUTH_TOKEN_REQUEST_ERROR',
message: axiosError.message,
error,
data: error,
stack: axiosError.stack,
});
});
Expand Down
12 changes: 1 addition & 11 deletions apps/server/src/shared/testing/factory/axios-error.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { axiosResponseFactory } from './axios-response.factory';
import { BaseFactory } from './base.factory';

type AxiosErrorProps<T> = {
data: T;
status: number;
statusText: string;
headers: AxiosHeaders;
Expand All @@ -20,8 +19,6 @@ type AxiosErrorProps<T> = {
};

class AxiosErrorImp<T> implements AxiosError {
data: T;

status: number;

statusText: string;
Expand All @@ -43,7 +40,6 @@ class AxiosErrorImp<T> implements AxiosError {
stack: string;

constructor(props: AxiosErrorProps<T>) {
this.data = props.data;
this.status = props.status;
this.statusText = props.statusText;
this.headers = new AxiosHeaders(props.headers);
Expand Down Expand Up @@ -73,12 +69,6 @@ export class AxiosErrorFactory extends BaseFactory<AxiosErrorImp<unknown>, Axios

export const axiosErrorFactory = AxiosErrorFactory.define(AxiosErrorImp, () => {
return {
data: {
error: {
message: 'Some error message',
code: '123',
},
},
status: HttpStatus.BAD_REQUEST,
statusText: 'Bad Request',
headers: new AxiosHeaders(),
Expand All @@ -88,6 +78,6 @@ export const axiosErrorFactory = AxiosErrorFactory.define(AxiosErrorImp, () => {
message: 'Bad Request',
name: 'BadRequest',
response: axiosResponseFactory.build({ status: HttpStatus.BAD_REQUEST }),
stack: '',
stack: 'mockStack',
};
});

0 comments on commit 39164c6

Please sign in to comment.