Skip to content

Commit

Permalink
move error handling to mapAclDomainToHttpError()
Browse files Browse the repository at this point in the history
  • Loading branch information
hotzevzl committed Jul 27, 2023
1 parent 8c36c5d commit 07d51c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from '@marxan-api/modules/projects/projects.service';
import { UpdateProjectTagDTO } from '@marxan-api/modules/projects/dto/update-project-tag.dto';

export const featureNotFound = Symbol('feature not found');
export const featureNotFoundWithinProject = Symbol(
'feature not found within project',
);
Expand Down
20 changes: 5 additions & 15 deletions api/apps/api/src/modules/geo-features/geo-features.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { inlineJobTag } from '@marxan-api/dto/inline-job-tag';
import { RequestWithAuthenticatedUser } from '@marxan-api/app.controller';
import { UpdateFeatureNameDto } from './dto/update-feature-name.dto';
import { isLeft } from 'fp-ts/Either';
import { mapAclDomainToHttpError } from '@marxan-api/utils/acl.utils';

@IsMissingAclImplementation()
@UseGuards(JwtAuthGuard)
Expand Down Expand Up @@ -100,21 +101,10 @@ export class GeoFeaturesController {
);

if (isLeft(result)) {
switch (result.left) {
case featureNotFound:
throw new NotFoundException(`Feature with id ${featureId} not found`);
case featureNotEditable:
throw new ForbiddenException(
`Feature with id ${featureId} is not editable`,
);
case featureNameAlreadyInUse:
throw new ForbiddenException(
`Feature with id ${featureId} cannot be updated: name is already in use (${body.featureClassName})`,
);
default:
const _exhaustiveCheck: never = result.left;
throw _exhaustiveCheck;
}
throw mapAclDomainToHttpError(result.left, {
featureId,
featureClassName: body.featureClassName,
});
} else {
return this.geoFeatureService.serialize(result.right);
}
Expand Down
15 changes: 14 additions & 1 deletion api/apps/api/src/utils/acl.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ import {
} from '@marxan-api/modules/access-control/access-control.types';
import {
featureDataCannotBeUploadedWithCsv,
featureNameAlreadyInUse,
featureNotEditable,
featureNotFound,
importedFeatureNameAlreadyExist,
missingPuidColumnInFeatureAmountCsvUpload,
unknownPuidsInFeatureAmountCsvUpload,
Expand All @@ -94,7 +97,6 @@ import {
} from '@marxan-api/modules/geo-features/import/csv.parser';
import {
featureNotEditableByUserWithinProject,
featureNotFound,
featureNotFoundWithinProject,
tagNotFoundForProject,
} from '@marxan-api/modules/geo-feature-tags/geo-feature-tags.service';
Expand All @@ -108,6 +110,7 @@ interface ErrorHandlerOptions {
scenarioId?: string;
userId?: string;
exportId?: string;
featureClassName?: string;
}

export const mapAclDomainToHttpError = (
Expand Down Expand Up @@ -155,9 +158,11 @@ export const mapAclDomainToHttpError = (
| typeof projectNotFoundForExport
| typeof projectIsNotPublished
| typeof deleteScenarioFailed
| typeof featureNameAlreadyInUse
| typeof featureNotFound
| typeof featureNotFoundWithinProject
| typeof featureNotEditableByUserWithinProject
| typeof featureNotEditable
| typeof projectNotFound
| typeof projectNotEditable
| typeof projectNotVisible
Expand Down Expand Up @@ -319,6 +324,10 @@ export const mapAclDomainToHttpError = (
return new BadRequestException(
`Scenario ${options?.scenarioId} and associated resources could not be deleted.`,
);
case featureNameAlreadyInUse:
throw new ForbiddenException(
`Feature with id ${options?.featureId} cannot be updated: name is already in use (${options?.featureClassName})`,
);
case featureNotFound:
throw new NotFoundException(
`Feature with id ${options?.featureId} not found`,
Expand All @@ -331,6 +340,10 @@ export const mapAclDomainToHttpError = (
throw new ForbiddenException(
`Feature with id ${options?.featureId} is not editable by user ${options?.userId} within Project with id ${options?.projectId}`,
);
case featureNotEditable:
throw new ForbiddenException(
`Feature with id ${options?.featureId} is not editable`,
);
case projectNotEditable:
throw new ForbiddenException(
`Project with id ${options?.projectId} is not editable by user ${options?.userId}`,
Expand Down

0 comments on commit 07d51c3

Please sign in to comment.