Add unique constraint check to StoryService

This commit is contained in:
Jerko Steiner 2019-03-12 11:50:19 +05:00
parent a4be36d159
commit 406858cf2d
2 changed files with 9 additions and 1 deletions

View File

@ -20,6 +20,12 @@ export class ErrorTransformer {
} }
return match === err.message return match === err.message
} }
throwIfNotMatch(err: Error) {
if (!this.isMatch(err)) {
throw err
}
}
} }
export const UniqueTransformer = new ErrorTransformer(400, /unique/i) export const UniqueTransformer = new ErrorTransformer(400, /unique/i)

View File

@ -4,6 +4,7 @@ import {ISiteService} from '../site/ISiteService'
import {IStoryService} from './IStoryService' import {IStoryService} from './IStoryService'
import {ITransactionManager} from '../database/ITransactionManager' import {ITransactionManager} from '../database/ITransactionManager'
import {Story} from '../entities/Story' import {Story} from '../entities/Story'
import {UniqueTransformer} from '../error/ErrorTransformer'
export class StoryService extends BaseService implements IStoryService { export class StoryService extends BaseService implements IStoryService {
constructor( constructor(
@ -35,7 +36,8 @@ export class StoryService extends BaseService implements IStoryService {
siteId: site.id, siteId: site.id,
}) })
} catch (err) { } catch (err) {
// TODO check if unique constrint error // throw if not a unique constraint error
UniqueTransformer.throwIfNotMatch(err)
// This could happen if there are two concurrent requests coming in at // This could happen if there are two concurrent requests coming in at
// the same time, and they both cannot find the story, then decide to // the same time, and they both cannot find the story, then decide to