create custom error for release actions

This commit is contained in:
Fernando Chavez 2024-03-14 16:16:00 +01:00
parent 9403426835
commit 3523c1f7cf
2 changed files with 11 additions and 6 deletions

View File

@ -14,6 +14,7 @@ import type {
} from '../../../shared/contracts/release-actions';
import { getService } from '../utils';
import { RELEASE_ACTION_MODEL_UID } from '../constants';
import { AlreadyOnReleaseError } from '../services/validation';
const releaseActionController = {
async create(ctx: Koa.Context) {
@ -48,11 +49,8 @@ const releaseActionController = {
return action;
} catch (error) {
if (
error instanceof errors.ValidationError &&
error.message ===
`Entry with id ${releaseActionArgs.entry.id} and contentType ${releaseActionArgs.entry.contentType} already exists in release with id ${releaseId}`
) {
// If the entry is already in the release, we don't want to throw an error, so we catch and ignore it
if (error instanceof AlreadyOnReleaseError) {
return null;
}

View File

@ -5,6 +5,13 @@ import type { Release, CreateRelease, UpdateRelease } from '../../../shared/cont
import type { CreateReleaseAction } from '../../../shared/contracts/release-actions';
import { RELEASE_MODEL_UID } from '../constants';
export class AlreadyOnReleaseError extends errors.ApplicationError<'AlreadyOnReleaseError'> {
constructor(message: string) {
super(message);
this.name = 'AlreadyOnReleaseError';
}
}
const createReleaseValidationService = ({ strapi }: { strapi: LoadedStrapi }) => ({
async validateUniqueEntry(
releaseId: CreateReleaseAction.Request['params']['releaseId'],
@ -29,7 +36,7 @@ const createReleaseValidationService = ({ strapi }: { strapi: LoadedStrapi }) =>
);
if (isEntryInRelease) {
throw new errors.ValidationError(
throw new AlreadyOnReleaseError(
`Entry with id ${releaseActionArgs.entry.id} and contentType ${releaseActionArgs.entry.contentType} already exists in release with id ${releaseId}`
);
}