From 41242d77d5d9fdb087c6601505284c84dce99964 Mon Sep 17 00:00:00 2001 From: Maximilian Kreuzkam Date: Fri, 24 Nov 2023 16:20:57 +0100 Subject: [PATCH] Add missing error mappings. --- ...domain-to-schulconnex-error.mapper.spec.ts | 5 ++ .../domain-to-schulconnex-error.mapper.ts | 51 +++++++++++++++++-- src/shared/error/error.module.spec.ts | 28 ++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 src/shared/error/error.module.spec.ts diff --git a/src/shared/error/domain-to-schulconnex-error.mapper.spec.ts b/src/shared/error/domain-to-schulconnex-error.mapper.spec.ts index 39fe0acb3..d61683473 100644 --- a/src/shared/error/domain-to-schulconnex-error.mapper.spec.ts +++ b/src/shared/error/domain-to-schulconnex-error.mapper.spec.ts @@ -11,6 +11,7 @@ import { MappingError } from './mapping.error.js'; import { MismatchedRevisionError } from './mismatched-revision.error.js'; import { PersonAlreadyExistsError } from './person-already-exists.error.js'; import { SchulConnexError } from './schul-connex.error.js'; +import { HttpException } from '@nestjs/common'; describe('DomainToSchulConnexErrorMapper', () => { let module: TestingModule; @@ -69,5 +70,9 @@ describe('DomainToSchulConnexErrorMapper', () => { sut.map({} as PersonAlreadyExistsError, PersonAlreadyExistsError, SchulConnexError), ).not.toThrow(MappingError); }); + + it('should map SchulConnexError to HttpException', () => { + expect(() => sut.map({} as SchulConnexError, SchulConnexError, HttpException)).not.toThrow(MappingError); + }); }); }); diff --git a/src/shared/error/domain-to-schulconnex-error.mapper.ts b/src/shared/error/domain-to-schulconnex-error.mapper.ts index da9633310..e0a25054a 100644 --- a/src/shared/error/domain-to-schulconnex-error.mapper.ts +++ b/src/shared/error/domain-to-schulconnex-error.mapper.ts @@ -1,6 +1,7 @@ -import { Mapper, MappingProfile, createMap, forMember, fromValue } from '@automapper/core'; +import 'reflect-metadata'; +import { Mapper, MappingProfile, constructUsing, createMap, forMember, fromValue } from '@automapper/core'; import { AutomapperProfile, getMapperToken } from '@automapper/nestjs'; -import { Inject } from '@nestjs/common'; +import { HttpException, Inject } from '@nestjs/common'; import { EntityCouldNotBeCreated } from './entity-could-not-be-created.error.js'; import { EntityCouldNotBeUpdated } from './entity-could-not-be-updated.error.js'; import { EntityNotFoundError } from './entity-not-found.error.js'; @@ -16,8 +17,30 @@ export class DomainToSchulConnexErrorMapper extends AutomapperProfile { public override get profile(): MappingProfile { return (mapper: Mapper) => { - createMap(mapper, EntityCouldNotBeCreated, SchulConnexError); - createMap(mapper, EntityCouldNotBeUpdated, SchulConnexError); + createMap( + mapper, + EntityCouldNotBeCreated, + SchulConnexError, + forMember((dest: SchulConnexError) => dest.code, fromValue(500)), + forMember((dest: SchulConnexError) => dest.subcode, fromValue('00')), + forMember((dest: SchulConnexError) => dest.titel, fromValue('Interner Serverfehler')), + forMember( + (dest: SchulConnexError) => dest.beschreibung, + fromValue('Es ist ein interner Fehler aufgetreten. Entität konnte nicht erstellt werden.'), + ), + ); + createMap( + mapper, + EntityCouldNotBeUpdated, + SchulConnexError, + forMember((dest: SchulConnexError) => dest.code, fromValue(500)), + forMember((dest: SchulConnexError) => dest.subcode, fromValue('00')), + forMember((dest: SchulConnexError) => dest.titel, fromValue('Interner Serverfehler')), + forMember( + (dest: SchulConnexError) => dest.beschreibung, + fromValue('Es ist ein interner Fehler aufgetreten. Entität konnte nicht aktualisiert werden.'), + ), + ); createMap( mapper, @@ -32,7 +55,18 @@ export class DomainToSchulConnexErrorMapper extends AutomapperProfile { ), ); - createMap(mapper, KeycloakClientError, SchulConnexError); + createMap( + mapper, + KeycloakClientError, + SchulConnexError, + forMember((dest: SchulConnexError) => dest.code, fromValue(500)), + forMember((dest: SchulConnexError) => dest.subcode, fromValue('00')), + forMember((dest: SchulConnexError) => dest.titel, fromValue('Interner Serverfehler')), + forMember( + (dest: SchulConnexError) => dest.beschreibung, + fromValue('Es ist ein interner Fehler aufgetreten.'), + ), + ); createMap( mapper, @@ -64,6 +98,13 @@ export class DomainToSchulConnexErrorMapper extends AutomapperProfile { fromValue('Die Anfrage ist fehlerhaft: Die Person existiert bereits.'), ), ); + + createMap( + mapper, + SchulConnexError, + HttpException, + constructUsing((scError: SchulConnexError) => new HttpException(scError, scError.code)), + ); }; } } diff --git a/src/shared/error/error.module.spec.ts b/src/shared/error/error.module.spec.ts new file mode 100644 index 000000000..d98eeba54 --- /dev/null +++ b/src/shared/error/error.module.spec.ts @@ -0,0 +1,28 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { ConfigTestModule, DatabaseTestModule, MapperTestModule } from '../../../test/utils/index.js'; +import { DomainToSchulConnexErrorMapper } from './domain-to-schulconnex-error.mapper.js'; +import { ErrorModule } from './error.module.js'; + +describe('ErrorModule', () => { + let module: TestingModule; + + beforeAll(async () => { + module = await Test.createTestingModule({ + imports: [ConfigTestModule, DatabaseTestModule.forRoot(), MapperTestModule, ErrorModule], + }).compile(); + }); + + afterAll(async () => { + await module.close(); + }); + + it('should be defined', () => { + expect(module).toBeDefined(); + }); + + describe('when module is initialized', () => { + it('should resolve DomainToSchulConnexErrorMapper', () => { + expect(module.get(DomainToSchulConnexErrorMapper)).toBeInstanceOf(DomainToSchulConnexErrorMapper); + }); + }); +});