Skip to content

Commit

Permalink
Handle errors with duplicate artifact catalog ids
Browse files Browse the repository at this point in the history
  • Loading branch information
nicou committed Jul 4, 2024
1 parent 5153473 commit 4bae605
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/routes/science.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getData, setData } from '../routes/data';
import { getEmptyEpsilonClient } from '../integrations/emptyepsilon/client';
import { get, set, cloneDeep, pick } from 'lodash';
import { shipLogger } from '../models/log';
import { StatusCodes } from '@/utils/http';
import { logger } from '@/logger';
const router = new Router();

/**
Expand Down Expand Up @@ -58,7 +60,14 @@ router.put('/artifact', handleAsyncErrors(async (req, res) => {
let artifact;
if (id) artifact = await Artifact.forge({ id }).fetch();
if (!artifact) {
artifact = await Artifact.forge().save(req.body, { method: 'insert' });
try {
artifact = await Artifact.forge().save(req.body, { method: 'insert' });
} catch (err) {
if (err.message.includes('duplicate key value violates unique constraint "artifact_catalog_id_unique"')) {
logger.warn(`Attempted to insert artifact with duplicate catalog ID '${req.body.catalog_id}'`);
return res.status(StatusCodes.CONFLICT).json({ message: 'Catalog ID must be unique' });
}
}
} else {
await artifact.save(req.body, { method: 'update', patch: true });
}
Expand Down

0 comments on commit 4bae605

Please sign in to comment.