Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-5489 - For loggables it should possible to pass unknown cause error #4501

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 88 additions & 11 deletions apps/server/src/core/error/filter/global-error.filter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-disable promise/valid-params */
import { NotFound } from '@feathersjs/errors';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ArgumentsHost, BadRequestException, HttpStatus } from '@nestjs/common';
import { ArgumentsHost, BadRequestException, HttpStatus, InternalServerErrorException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { BusinessError } from '@shared/common';
import { ErrorLogger, ErrorLogMessage, Loggable, LogMessage, ValidationErrorLogMessage } from '@src/core/logger';
import { Response } from 'express';
import util from 'util';
import { ErrorResponse } from '../dto';
import { ErrorLoggable } from '../loggable/error.loggable';
import { ErrorUtils } from '../utils';
import { GlobalErrorFilter } from './global-error.filter';

class SampleBusinessError extends BusinessError {
Expand Down Expand Up @@ -42,9 +43,27 @@
}
}

class LoggableExceptionWithCause extends InternalServerErrorException implements Loggable {
bischofmax marked this conversation as resolved.
Show resolved Hide resolved
bischofmax marked this conversation as resolved.
Show resolved Hide resolved
constructor(private readonly testValue: string, error?: unknown) {
super(ErrorUtils.createHttpExceptionOptions(error));
}

getLogMessage(): ErrorLogMessage {
const message: ErrorLogMessage = {
type: 'WITH_CAUSE',
bischofmax marked this conversation as resolved.
Show resolved Hide resolved
stack: this.stack,
bischofmax marked this conversation as resolved.
Show resolved Hide resolved
data: {
testValue: this.testValue,
},
};

return message;
}
}

describe('GlobalErrorFilter', () => {
let module: TestingModule;
let service: GlobalErrorFilter<any>;

Check warning on line 66 in apps/server/src/core/error/filter/global-error.filter.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Unexpected any. Specify a different type
let logger: DeepMocked<ErrorLogger>;

beforeAll(async () => {
Expand Down Expand Up @@ -304,24 +323,82 @@
).toBeCalledWith(expectedResponse);
});
});

describe('when error has a cause error', () => {
const setup = () => {
const causeError = new Error('Cause error');
const error = new LoggableExceptionWithCause('test', causeError);
const expectedResponse = new ErrorResponse(
'WITH_CAUSE',
'With Cause',
'Loggable Exception With Cause',
HttpStatus.INTERNAL_SERVER_ERROR
);

const argumentsHost = setupHttpArgumentsHost();

return { error, argumentsHost, expectedResponse };
};

it('should set response status appropriately', () => {
const { error, argumentsHost } = setup();

service.catch(error, argumentsHost);

expect(argumentsHost.switchToHttp().getResponse<Response>().status).toBeCalledWith(
HttpStatus.INTERNAL_SERVER_ERROR
);
});

it('should send appropriate error response', () => {
const { error, argumentsHost, expectedResponse } = setup();

service.catch(error, argumentsHost);

expect(
argumentsHost.switchToHttp().getResponse<Response>().status(HttpStatus.INTERNAL_SERVER_ERROR).json
).toBeCalledWith(expectedResponse);
});
});
});

describe('when context is rmq', () => {
const setup = () => {
const argumentsHost = createMock<ArgumentsHost>();
argumentsHost.getType.mockReturnValueOnce('rmq');
describe('Name of the group', () => {
bischofmax marked this conversation as resolved.
Show resolved Hide resolved
const setup = () => {
const argumentsHost = createMock<ArgumentsHost>();
argumentsHost.getType.mockReturnValueOnce('rmq');

const error = new Error();
const error = new Error();

return { error, argumentsHost };
};
return { error, argumentsHost };
};

it('should return an RpcMessage with the error', () => {
const { error, argumentsHost } = setup();
it('should return an RpcMessage with the error', () => {
const { error, argumentsHost } = setup();

const result = service.catch(error, argumentsHost);
const result = service.catch(error, argumentsHost);

expect(result).toEqual({ message: undefined, error });
expect(result).toEqual({ message: undefined, error });
});
});

describe('when error is a LoggableError', () => {
const setup = () => {
const causeError = new Error('Cause error');
const error = new LoggableExceptionWithCause('test', causeError);
const argumentsHost = createMock<ArgumentsHost>();
argumentsHost.getType.mockReturnValueOnce('rmq');

return { error, argumentsHost };
};

it('should return appropriate error', () => {
const { error, argumentsHost } = setup();

const result = service.catch(error, argumentsHost);

expect(result).toEqual({ message: undefined, error });
});
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wäre hier noch ein describe sinnvoll wo wir ein Szenario wie "describe('when context is other than rmq and http'," testen?

});
Expand Down
Loading