-
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.
fix: remove form.msgSrvcName validation and add human error recovery …
…message (#1235)
- Loading branch information
Showing
5 changed files
with
69 additions
and
26 deletions.
There are no files selected for viewing
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 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 |
---|---|---|
|
@@ -8,6 +8,15 @@ import { | |
DatabaseValidationError, | ||
} from '../modules/core/core.errors' | ||
|
||
/** | ||
* Exported for testing. | ||
* Format error recovery message to be returned to client. | ||
* @param errMsg the error message | ||
*/ | ||
export const formatErrorRecoveryMessage = (errMsg: string): string => { | ||
return `Error: [${errMsg}]. Please refresh and try again. If you still need help, email us at [email protected].` | ||
} | ||
|
||
export const getMongoErrorMessage = ( | ||
err?: unknown, | ||
// Default error message if no more specific error | ||
|
@@ -21,9 +30,11 @@ export const getMongoErrorMessage = ( | |
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.' | ||
return formatErrorRecoveryMessage( | ||
'Your form is too large to be supported by the system.', | ||
) | ||
default: | ||
return defaultErrorMessage | ||
return formatErrorRecoveryMessage(defaultErrorMessage) | ||
} | ||
} | ||
|
||
|
@@ -34,18 +45,20 @@ export const getMongoErrorMessage = ( | |
.map((err) => err.message) | ||
.join(', ') | ||
|
||
return joinedMessage ?? err.message ?? defaultErrorMessage | ||
return formatErrorRecoveryMessage( | ||
joinedMessage ?? err.message ?? defaultErrorMessage, | ||
) | ||
} | ||
|
||
if (err instanceof MongooseError || err instanceof Error) { | ||
return err.message ?? defaultErrorMessage | ||
return formatErrorRecoveryMessage(err.message ?? defaultErrorMessage) | ||
} | ||
|
||
if (typeof err === 'string') { | ||
return err ?? defaultErrorMessage | ||
return formatErrorRecoveryMessage(err ?? defaultErrorMessage) | ||
} | ||
|
||
return defaultErrorMessage | ||
return formatErrorRecoveryMessage(defaultErrorMessage) | ||
} | ||
|
||
/** | ||
|
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 |
---|---|---|
@@ -1,51 +1,81 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
import { MongoError } from 'mongodb' | ||
import { Error as MongooseError } from 'mongoose' | ||
|
||
import { getMongoErrorMessage } from 'src/app/utils/handle-mongo-error' | ||
import { | ||
formatErrorRecoveryMessage, | ||
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', () => { | ||
it('should return formatted string if error is string', () => { | ||
const err = 'something failed' | ||
expect(getMongoErrorMessage(err)).toEqual(err) | ||
expect(getMongoErrorMessage(err)).toEqual(formatErrorRecoveryMessage(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.', | ||
formatErrorRecoveryMessage( | ||
'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.', | ||
formatErrorRecoveryMessage( | ||
'An unexpected error happened. Please try again.', | ||
), | ||
) // Preset default error message | ||
expect(getMongoErrorMessage(err, 'new error message')).toEqual( | ||
'new error message', | ||
formatErrorRecoveryMessage('new error message'), | ||
) // Changed default error message | ||
}) | ||
|
||
it('should join all error messages into a single message if available.', () => { | ||
// @ts-ignore | ||
const err = new MongooseError.ValidationError() | ||
// @ts-ignore | ||
const err1 = new MongooseError.ValidatorError({}) | ||
err1.message = 'abc' | ||
// @ts-ignore | ||
const err2 = new MongooseError.ValidatorError({}) | ||
err2.message = 'def' | ||
err.errors = { err1, err2 } | ||
expect(getMongoErrorMessage(err)).toEqual('abc, def') | ||
expect(getMongoErrorMessage(err)).toEqual( | ||
formatErrorRecoveryMessage('abc, def'), | ||
) | ||
}) | ||
|
||
it('should return error message for MongooseError', () => { | ||
const err = new MongooseError('mongooseError') | ||
expect(getMongoErrorMessage(err)).toEqual('mongooseError') | ||
expect(getMongoErrorMessage(err)).toEqual( | ||
formatErrorRecoveryMessage('mongooseError'), | ||
) | ||
}) | ||
}) | ||
|
||
describe('formatErrorRecoveryMessage', () => { | ||
it('should format given string', () => { | ||
// Arrange | ||
const errorString = 'some test string' | ||
|
||
// Act | ||
const actual = formatErrorRecoveryMessage(errorString) | ||
|
||
// Assert | ||
expect(actual).toEqual( | ||
`Error: [${errorString}]. Please refresh and try again. If you still need help, email us at [email protected].`, | ||
) | ||
}) | ||
}) | ||
}) |