Skip to content

Commit

Permalink
Add missing error mappings.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreuzkam-cap committed Nov 24, 2023
1 parent 1e51277 commit 41242d7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/shared/error/domain-to-schulconnex-error.mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
});
});
51 changes: 46 additions & 5 deletions src/shared/error/domain-to-schulconnex-error.mapper.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)),
);
};
}
}
28 changes: 28 additions & 0 deletions src/shared/error/error.module.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 41242d7

Please sign in to comment.