Skip to content

Commit

Permalink
good good?
Browse files Browse the repository at this point in the history
  • Loading branch information
Loki-Afro committed Oct 24, 2024
1 parent 6b4d739 commit 5a3b420
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
13 changes: 12 additions & 1 deletion apps/server/src/core/error/loggable/error.loggable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NotFound } from '@feathersjs/errors';
import { BadRequestException, HttpStatus, ValidationError } from '@nestjs/common';
import { ApiProperty } from '@nestjs/swagger';
import { ApiValidationError, BusinessError } from '@shared/common';
import { ApiValidationError, BusinessError, ErrorLogMessage } from '@shared/common';
import { PrivacyProtect } from '@shared/controller';
import { ErrorLoggable } from './error.loggable';

Expand Down Expand Up @@ -158,5 +158,16 @@ describe('ErrorLoggable', () => {
expect(message).toEqual(expectedMessage);
});
});

describe('when error is of unknown type', () => {
it('should return ErrorLogMessage with appropriate error type', () => {
const errorLoggable = new ErrorLoggable('something broke');

const message = errorLoggable.getLogMessage() as ErrorLogMessage;
expect(message.data).toBeUndefined();
expect(message.error).toBeInstanceOf(Error);
expect(message.type).toBe('Unhandled or Unknown Error');
});
});
});
});
23 changes: 16 additions & 7 deletions apps/server/src/core/error/loggable/error.loggable.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import { ValidationError } from '@nestjs/common';
import { ApiValidationError } from '@shared/common';
import { getMetadataStorage } from 'class-validator';
import util from 'util';
import { Loggable } from '../../logger/interfaces';
import { ErrorLogMessage, LogMessageDataObject, ValidationErrorLogMessage } from '../../logger/types';
import { ErrorUtils } from '../utils/error.utils';

export class ErrorLoggable implements Loggable {
constructor(private readonly error: Error, private readonly data?: LogMessageDataObject) {}
readonly actualError: Error;

constructor(private readonly error: unknown, private readonly data?: LogMessageDataObject) {
if (this.error instanceof Error) {
this.actualError = <Error>error;
} else {
this.actualError = new Error(util.inspect(error));
}
}

private readonly classValidatorMetadataStorage = getMetadataStorage();

getLogMessage(): ErrorLogMessage | ValidationErrorLogMessage {
let logMessage: ErrorLogMessage | ValidationErrorLogMessage = {
error: this.error,
error: this.actualError,
type: '',
data: this.data,
};

if (this.error instanceof ApiValidationError) {
logMessage = this.createLogMessageForValidationErrors(this.error);
} else if (ErrorUtils.isFeathersError(this.error)) {
if (this.actualError instanceof ApiValidationError) {
logMessage = this.createLogMessageForValidationErrors(this.actualError);
} else if (ErrorUtils.isFeathersError(this.actualError)) {
logMessage.type = 'Feathers Error';
} else if (ErrorUtils.isBusinessError(this.error)) {
} else if (ErrorUtils.isBusinessError(this.actualError)) {
logMessage.type = 'Business Error';
} else if (ErrorUtils.isNestHttpException(this.error)) {
} else if (ErrorUtils.isNestHttpException(this.actualError)) {
logMessage.type = 'Technical Error';
} else {
logMessage.type = 'Unhandled or Unknown Error';
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/modules/health/uc/health.uc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';

import { Logger } from '@src/core/logger';
import { GenericErrorLoggable } from '@shared/common/loggable';
import { ErrorLoggable } from '@src/core/error/loggable';
import { HealthService } from '../service';
import { HealthConfig } from '../health.config';
import { HealthStatuses, HealthStatusCheck, HealthStatus } from '../domain';
Expand Down Expand Up @@ -74,7 +74,7 @@ export class HealthUC {

await this.healthService.upsertHealthCheckById(healthCheckID);
} catch (error) {
this.logger.warning(new GenericErrorLoggable(error));
this.logger.warning(new ErrorLoggable(error));
// If any error occurred in the database operation execution it should be indicated
// as a MongoDB check failure (and thus the whole health check should fail).

Expand Down
18 changes: 0 additions & 18 deletions apps/server/src/shared/common/loggable/generic-error-loggable.ts

This file was deleted.

1 change: 0 additions & 1 deletion apps/server/src/shared/common/loggable/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { ReferencedEntityNotFoundLoggable } from './referenced-entity-not-found-loggable';
export { Loggable, LoggableMessage } from './interfaces';
export { GenericErrorLoggable } from './generic-error-loggable';

0 comments on commit 5a3b420

Please sign in to comment.