Skip to content

Commit

Permalink
EW-645 adding module and mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
psachmann committed Nov 23, 2023
1 parent f949b9d commit 1e51277
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
52 changes: 47 additions & 5 deletions src/shared/error/domain-to-schulconnex-error.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Mapper, MappingProfile, createMap } from '@automapper/core';
import { Mapper, MappingProfile, createMap, forMember, fromValue } from '@automapper/core';
import { AutomapperProfile, getMapperToken } from '@automapper/nestjs';
import { Inject } from '@nestjs/common';
import { SchulConnexError } from './schul-connex.error.js';
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';
import { KeycloakClientError } from './keycloak-client.error.js';
import { MismatchedRevisionError } from './mismatched-revision.error.js';
import { PersonAlreadyExistsError } from './person-already-exists.error.js';
import { SchulConnexError } from './schul-connex.error.js';

export class DomainToSchulConnexErrorMapper extends AutomapperProfile {
public constructor(@Inject(getMapperToken()) mapper: Mapper) {
Expand All @@ -18,10 +18,52 @@ export class DomainToSchulConnexErrorMapper extends AutomapperProfile {
return (mapper: Mapper) => {
createMap(mapper, EntityCouldNotBeCreated, SchulConnexError);
createMap(mapper, EntityCouldNotBeUpdated, SchulConnexError);
createMap(mapper, EntityNotFoundError, SchulConnexError);

createMap(
mapper,
EntityNotFoundError,
SchulConnexError,
forMember((dest: SchulConnexError) => dest.code, fromValue(404)),
forMember((dest: SchulConnexError) => dest.subcode, fromValue('01')),
forMember((dest: SchulConnexError) => dest.titel, fromValue('Angefragte Entität existiert nicht')),
forMember(
(dest: SchulConnexError) => dest.beschreibung,
fromValue('Die angeforderte Entität existiert nicht.'),
),
);

createMap(mapper, KeycloakClientError, SchulConnexError);
createMap(mapper, MismatchedRevisionError, SchulConnexError);
createMap(mapper, PersonAlreadyExistsError, SchulConnexError);

createMap(
mapper,
MismatchedRevisionError,
SchulConnexError,
forMember((dest: SchulConnexError) => dest.code, fromValue(409)),
forMember((dest: SchulConnexError) => dest.subcode, fromValue('00')),
forMember(
(dest: SchulConnexError) => dest.titel,
fromValue('Konflikt mit dem aktuellen Zustand der Resource'),
),
forMember(
(dest: SchulConnexError) => dest.beschreibung,
fromValue(
'Die Entität wurde eventuell durch Dritte verändert. Die Revisionsnummer stimmt nicht überein.',
),
),
);

createMap(
mapper,
PersonAlreadyExistsError,
SchulConnexError,
forMember((dest: SchulConnexError) => dest.code, fromValue(400)),
forMember((dest: SchulConnexError) => dest.subcode, fromValue('00')),
forMember((dest: SchulConnexError) => dest.titel, fromValue('Fehlerhafte Anfrage')),
forMember(
(dest: SchulConnexError) => dest.beschreibung,
fromValue('Die Anfrage ist fehlerhaft: Die Person existiert bereits.'),
),
);
};
}
}
8 changes: 8 additions & 0 deletions src/shared/error/error.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { DomainToSchulConnexErrorMapper } from './domain-to-schulconnex-error.mapper.js';

@Module({
providers: [DomainToSchulConnexErrorMapper],
exports: [DomainToSchulConnexErrorMapper],
})
export class ErrorModule {}
20 changes: 13 additions & 7 deletions src/shared/error/schul-connex.error.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { HttpException } from '@nestjs/common';

type SchulConnexErrorProps = {
statusCode: number;
code: number;
subcode: string;
title: string;
description: string;
titel: string;
beschreibung: string;
};

export class SchulConnexError extends HttpException {
export class SchulConnexError {
public constructor(props: SchulConnexErrorProps) {
super(props, props.statusCode);
Object.assign(this, props);
}

public readonly code!: number;

public readonly subcode!: string;

public readonly titel!: string;

public readonly beschreibung!: string;
}

0 comments on commit 1e51277

Please sign in to comment.