-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert errors.server.controller to typescript utility func…
…tion (#602) * chore: rename errors server controller to typescript * chore: add @types/mongodb as devdependency * chore: define MongoError interface * chore: typescripify errors server controller * chore: rename errors server controller spec to ts * chore: clean up errors.server.controller.spec.ts * refactor: use destructuring assignment for isEmpty * chore: remove types from JSDocs * chore: remove defaultErrorMessage from exports * chore: remove mongoDuplicateKeyError as only UserSchema has unique key and that is already validated * refactor: use arrow function for getMongoErrorMessage * refactor: clean up getMongoErrorMessage * chore: shift errors.server.controller to utility function * chore: update tests * chore: shift defaultErrorMessage to function parameter
- Loading branch information
Showing
9 changed files
with
96 additions
and
137 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { MongoError } from 'mongodb' | ||
import { Error as MongooseError } from 'mongoose' | ||
|
||
export const getMongoErrorMessage = ( | ||
err?: MongoError | MongooseError | string, | ||
// Default error message if no more specific error | ||
defaultErrorMessage = 'An unexpected error happened. Please try again.', | ||
): string => { | ||
if (!err) { | ||
return '' | ||
} | ||
|
||
// Handle base Mongo engine errors | ||
if (err instanceof MongoError) { | ||
switch (err.code) { | ||
case 10334: // BSONObj size invalid error | ||
return 'Your form is too large to be supported by the system.' | ||
default: | ||
return defaultErrorMessage | ||
} | ||
} | ||
|
||
// Handle mongoose errors | ||
if (err instanceof MongooseError.ValidationError) { | ||
// Join all error messages into a single message if available. | ||
const joinedMessage = Object.values(err.errors) | ||
.map((err) => err.message) | ||
.join(', ') | ||
|
||
return joinedMessage ?? err.message ?? defaultErrorMessage | ||
} | ||
|
||
if (err instanceof MongooseError) { | ||
return err.message ?? defaultErrorMessage | ||
} | ||
|
||
return err ?? defaultErrorMessage | ||
} |
65 changes: 0 additions & 65 deletions
65
tests/unit/backend/controllers/errors.server.controller.spec.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { MongoError } from 'mongodb' | ||
import { Error as MongooseError } from 'mongoose' | ||
|
||
import { getMongoErrorMessage } from 'src/app/utils/handle-mongo-error' | ||
|
||
describe('handleMongoError', () => { | ||
describe('getMongoErrorMessage', () => { | ||
it('should return blank string if no error', () => { | ||
expect(getMongoErrorMessage()).toEqual('') | ||
}) | ||
|
||
it('should return string if error is string', () => { | ||
const err = 'something failed' | ||
expect(getMongoErrorMessage(err)).toEqual(err) | ||
}) | ||
|
||
it('should return form too large error message for error code 10334', () => { | ||
const err = new MongoError('MongoError') | ||
err.code = 10334 | ||
|
||
expect(getMongoErrorMessage(err)).toEqual( | ||
'Your form is too large to be supported by the system.', | ||
) | ||
}) | ||
|
||
it('should return default error message for other MongoError error code', () => { | ||
const err = new MongoError('MongoError') | ||
expect(getMongoErrorMessage(err)).toEqual( | ||
'An unexpected error happened. Please try again.', | ||
) // Preset default error message | ||
expect(getMongoErrorMessage(err, 'new error message')).toEqual( | ||
'new error message', | ||
) // Changed default error message | ||
}) | ||
|
||
it('should join all error messages into a single message if available.', () => { | ||
const err = new MongooseError.ValidationError() | ||
const err1 = new MongooseError.ValidatorError({}) | ||
err1.message = 'abc' | ||
const err2 = new MongooseError.ValidatorError({}) | ||
err2.message = 'def' | ||
err.errors = { err1, err2 } | ||
expect(getMongoErrorMessage(err)).toEqual('abc, def') | ||
}) | ||
|
||
it('should return error message for MongooseError', () => { | ||
const err = new MongooseError('mongooseError') | ||
expect(getMongoErrorMessage(err)).toEqual('mongooseError') | ||
}) | ||
}) | ||
}) |