-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hotfix/key thrown #110
Hotfix/key thrown #110
Changes from 6 commits
294147d
2ad3582
3bf78ee
868dace
b76b9fd
e04cc60
333f914
bdf83ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,14 +76,39 @@ fileSchema.virtual('fullExtension') | |
}); | ||
|
||
// handleE11000 is called when there is a duplicateKey Error | ||
const handleE11000 = function (error: MongoError, _: any, next: NextFunction) { | ||
const handleE11000 = function (error: MongoError, _: any, next: NextFunction) : void { | ||
if (error.name === 'MongoError' && error.code === 11000) { | ||
next(new KeyAlreadyExistsError(this.key)); | ||
const retMessage : string = getMongoErrorIndices(error); | ||
next(new KeyAlreadyExistsError(retMessage)); | ||
} else { | ||
next(); | ||
} | ||
}; | ||
|
||
/** | ||
* Extracts the indices names and values thrown by the duplicate key error. | ||
* @param error - the mongo error thrown. | ||
* @return string with the index names and values. | ||
*/ | ||
function getMongoErrorIndices(error: MongoError) : string { | ||
// extract the indices names in the MongoError | ||
const indicesRegex : RegExp = new RegExp(/index\:\ (?:.*\.)?\$?(?:([_a-z0-9]*)(?:_\d*)|([_a-z0-9]*))\s*dup key/i); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WTF is this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added documentation |
||
const indicesMatch : RegExpMatchArray = error.message.match(indicesRegex); | ||
let indexName : string = indicesMatch[1] || indicesMatch[2]; | ||
|
||
// prettify indices names | ||
const re : RegExp = new RegExp('_1_', 'g'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WTF is this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added documentation |
||
indexName = indexName.replace(re, ', '); | ||
|
||
// extract the indices values of the error thrown | ||
const valuesRE : RegExp = new RegExp(/{(.*?)}/); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WTF is this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added documentation |
||
const valuesMatch : RegExpMatchArray = error.message.match(valuesRE); | ||
let values : string = valuesMatch[0]; | ||
values = values.replace(new RegExp(' : ', 'g'), ' '); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WTF is this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added documentation |
||
|
||
return `${indexName} : ${values}`; | ||
} | ||
|
||
fileSchema.post('save', handleE11000); | ||
fileSchema.post('update', handleE11000); | ||
fileSchema.post('findOneAndUpdate', handleE11000); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -357,14 +357,37 @@ describe('File Logic', () => { | |
expect(file2.parent).to.equal(file1.parent); | ||
}); | ||
|
||
it('should throw an error when KEY already exist', async () => { | ||
it('should throw an error when KEY already exists', async () => { | ||
await FileService.create(bucket, 'tmp1', USER.id, 'text', null, KEY); | ||
await FileService.create(bucket, 'tmp2', USER.id, 'text', null, KEY) | ||
.should.eventually.be.rejectedWith(KeyAlreadyExistsError); | ||
}); | ||
|
||
}); | ||
|
||
describe('#updateById', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does updateById tests related to this issue/hotfix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created tests that were missing and are related to this issue |
||
it('should update a file', async () => { | ||
const file: IFile = await FileService.create(bucket, 'file.txt', USER.id, 'text', KEY2, KEY, size); | ||
await FileService.updateById(file.id, { name: 'changedFile', type: 'jpg' }); | ||
const changedFile : IFile = await FileService.getById(file.id); | ||
expect(changedFile.name).to.equal('changedFile'); | ||
expect(changedFile.type).to.equal('jpg'); | ||
}); | ||
|
||
it('should throw an error when changing a file to unique properties of another (trinity)', async () => { | ||
const file1: IFile = await FileService.create(bucket, 'file1.txt', USER.id, 'text', null, KEY); | ||
const file2: IFile = await FileService.create(bucket, 'file2.txt', USER.id, 'text', null, KEY2); | ||
await FileService.updateById(file1.id, { name: 'file2.txt' }).should.eventually.be.rejectedWith(KeyAlreadyExistsError); | ||
}); | ||
|
||
it('should throw an error when changing a file to unique properties of another (key)', async () => { | ||
const file1: IFile = await FileService.create(bucket, 'file1.txt', USER.id, 'text', null, KEY); | ||
const file2: IFile = await FileService.create(bucket, 'file2.txt', USER.id, 'text', null, KEY2); | ||
await FileService.updateById(file1.id, { key: KEY2 }).should.eventually.be.rejectedWith(KeyAlreadyExistsError); | ||
}); | ||
|
||
}); | ||
|
||
describe('#getByID', () => { | ||
it('should get an error when file does not exist', async () => { | ||
await FileService.getByKey(KEY).should.eventually.be.rejectedWith(FileNotFoundError); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,6 @@ export class FileService { | |
ownerID, | ||
size, | ||
_id: id, | ||
deleted: false, | ||
parent: parentID, | ||
}); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should create a different error for cases where the compound indexes already exist. We can accidentally confuse it with the
key
field.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to 'UniqueIndexExistsError'