Skip to content

Commit

Permalink
feat(api): handle Legal Document errors mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelleBonnemay authored Jan 8, 2025
1 parent f85245b commit 5636529
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/config/server-setup-error-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { certificationDomainErrorMappingConfiguration } from '../src/certificati
import { devcompDomainErrorMappingConfiguration } from '../src/devcomp/application/http-error-mapper-configuration.js';
import { evaluationDomainErrorMappingConfiguration } from '../src/evaluation/application/http-error-mapper-configuration.js';
import { authenticationDomainErrorMappingConfiguration } from '../src/identity-access-management/application/http-error-mapper-configuration.js';
import { legalDocumentsDomainErrorMappingConfiguration } from '../src/legal-documents/application/http-error-mapper-configuration.js';
import { organizationalEntitiesDomainErrorMappingConfiguration } from '../src/organizational-entities/application/http-error-mapper-configuration.js';
import { prescriptionDomainErrorMappingConfiguration } from '../src/prescription/shared/application/http-error-mapper-configuration.js';
import { profileDomainErrorMappingConfiguration } from '../src/profile/application/http-error-mapper-configuration.js';
Expand All @@ -18,6 +19,7 @@ const setupErrorHandling = function (server) {
...certificationDomainErrorMappingConfiguration,
...devcompDomainErrorMappingConfiguration,
...evaluationDomainErrorMappingConfiguration,
...legalDocumentsDomainErrorMappingConfiguration,
...prescriptionDomainErrorMappingConfiguration,
...schoolDomainErrorMappingConfiguration,
...profileDomainErrorMappingConfiguration,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { HttpErrors } from '../../shared/application/http-errors.js';
import { DomainErrorMappingConfiguration } from '../../shared/application/models/domain-error-mapping-configuration.js';
import { LegalDocumentInvalidDateError, LegalDocumentVersionNotFoundError } from '../domain/errors.js';

const legalDocumentsDomainErrorMappingConfiguration = [
{
name: LegalDocumentInvalidDateError.name,
httpErrorFn: (error) => new HttpErrors.UnprocessableEntityError(error.message, error.code, error.meta),
},
{
name: LegalDocumentVersionNotFoundError.name,
httpErrorFn: (error) => new HttpErrors.NotFoundError(error.message, error.code, error.meta),
},
].map((domainErrorMappingConfiguration) => new DomainErrorMappingConfiguration(domainErrorMappingConfiguration));

export { legalDocumentsDomainErrorMappingConfiguration };
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { legalDocumentsDomainErrorMappingConfiguration } from '../../../../../src/legal-documents/application/http-error-mapper-configuration.js';
import {
LegalDocumentInvalidDateError,
LegalDocumentVersionNotFoundError,
} from '../../../../../src/legal-documents/domain/errors.js';
import { HttpErrors } from '../../../../../src/shared/application/http-errors.js';
import { DomainErrorMappingConfiguration } from '../../../../../src/shared/application/models/domain-error-mapping-configuration.js';
import { expect } from '../../../../test-helper.js';

describe('Unit | Legal Documents | Application | HttpErrorMapperConfiguration', function () {
it('contains a list of HttpErrorMapper instances', function () {
// given
// when
// then
legalDocumentsDomainErrorMappingConfiguration.forEach((domainErrorMappingConfiguration) =>
expect(domainErrorMappingConfiguration).to.be.instanceOf(DomainErrorMappingConfiguration),
);
});

context('when mapping "LegalDocumentInvalidDateError"', function () {
it('returns an UnprocessableEntity Http Error', function () {
//given
const httpErrorMapper = legalDocumentsDomainErrorMappingConfiguration.find(
(httpErrorMapper) => httpErrorMapper.name === LegalDocumentInvalidDateError.name,
);

//when
const error = httpErrorMapper.httpErrorFn(new LegalDocumentInvalidDateError());

//then
expect(error).to.be.instanceOf(HttpErrors.UnprocessableEntityError);
});
});

context('when mapping "LegalDocumentVersionNotFoundError"', function () {
it('returns an NotFound Http Error', function () {
//given
const httpErrorMapper = legalDocumentsDomainErrorMappingConfiguration.find(
(httpErrorMapper) => httpErrorMapper.name === LegalDocumentVersionNotFoundError.name,
);

//when
const error = httpErrorMapper.httpErrorFn(new LegalDocumentVersionNotFoundError());

//then
expect(error).to.be.instanceOf(HttpErrors.NotFoundError);
});
});
});

0 comments on commit 5636529

Please sign in to comment.