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'; } from '../../../shared/contracts/release-actions';
import { getService } from '../utils'; import { getService } from '../utils';
import { RELEASE_ACTION_MODEL_UID } from '../constants'; import { RELEASE_ACTION_MODEL_UID } from '../constants';
import { AlreadyOnReleaseError } from '../services/validation';
const releaseActionController = { const releaseActionController = {
async create(ctx: Koa.Context) { async create(ctx: Koa.Context) {
@ -48,11 +49,8 @@ const releaseActionController = {
return action; return action;
} catch (error) { } catch (error) {
if ( // If the entry is already in the release, we don't want to throw an error, so we catch and ignore it
error instanceof errors.ValidationError && if (error instanceof AlreadyOnReleaseError) {
error.message ===
`Entry with id ${releaseActionArgs.entry.id} and contentType ${releaseActionArgs.entry.contentType} already exists in release with id ${releaseId}`
) {
return null; 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 type { CreateReleaseAction } from '../../../shared/contracts/release-actions';
import { RELEASE_MODEL_UID } from '../constants'; 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 }) => ({ const createReleaseValidationService = ({ strapi }: { strapi: LoadedStrapi }) => ({
async validateUniqueEntry( async validateUniqueEntry(
releaseId: CreateReleaseAction.Request['params']['releaseId'], releaseId: CreateReleaseAction.Request['params']['releaseId'],
@ -29,7 +36,7 @@ const createReleaseValidationService = ({ strapi }: { strapi: LoadedStrapi }) =>
); );
if (isEntryInRelease) { 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}` `Entry with id ${releaseActionArgs.entry.id} and contentType ${releaseActionArgs.entry.contentType} already exists in release with id ${releaseId}`
); );
} }