diff --git a/src/shared/error/domain-to-schulconnex-error.mapper.ts b/src/shared/error/domain-to-schulconnex-error.mapper.ts index 5cef03515..da9633310 100644 --- a/src/shared/error/domain-to-schulconnex-error.mapper.ts +++ b/src/shared/error/domain-to-schulconnex-error.mapper.ts @@ -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) { @@ -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.'), + ), + ); }; } } diff --git a/src/shared/error/error.module.ts b/src/shared/error/error.module.ts new file mode 100644 index 000000000..7f8bc47da --- /dev/null +++ b/src/shared/error/error.module.ts @@ -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 {} diff --git a/src/shared/error/schul-connex.error.ts b/src/shared/error/schul-connex.error.ts index 1d30bd612..cf91dad3d 100644 --- a/src/shared/error/schul-connex.error.ts +++ b/src/shared/error/schul-connex.error.ts @@ -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; }