Skip to content

Commit

Permalink
refactor: convert errors.server.controller to typescript utility func…
Browse files Browse the repository at this point in the history
…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
tshuli authored Nov 10, 2020
1 parent bb78d01 commit 0c9e65d
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 137 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
"@types/ip": "^1.1.0",
"@types/jest": "^26.0.15",
"@types/json-stringify-safe": "^5.0.0",
"@types/mongodb": "^3.5.27",
"@types/mongodb-uri": "^0.9.0",
"@types/mongoose": "^5.7.36",
"@types/node": "^14.14.7",
Expand Down
2 changes: 1 addition & 1 deletion src/app/controllers/admin-forms.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const JSONStream = require('JSONStream')
const { StatusCodes } = require('http-status-codes')

const logger = require('../../config/logger').createLoggerWithLabel(module)
const errorHandler = require('./errors.server.controller')
const errorHandler = require('../utils/handle-mongo-error')
const { createReqMeta } = require('../utils/request')
const { FormLogoState } = require('../../types')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const JSONStream = require('JSONStream')
const { StatusCodes } = require('http-status-codes')

const mongoose = require('mongoose')
const errorHandler = require('./errors.server.controller')
const errorHandler = require('../utils/handle-mongo-error')
const {
getEncryptSubmissionModel,
} = require('../models/submission.server.model')
Expand Down
66 changes: 0 additions & 66 deletions src/app/controllers/errors.server.controller.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/controllers/submissions.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const axios = require('axios')

const mongoose = require('mongoose')
const errorHandler = require('./errors.server.controller')
const errorHandler = require('../utils/handle-mongo-error')
const getSubmissionModel = require('../models/submission.server.model').default
const Submission = getSubmissionModel(mongoose)

Expand Down
38 changes: 38 additions & 0 deletions src/app/utils/handle-mongo-error.ts
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 tests/unit/backend/controllers/errors.server.controller.spec.js

This file was deleted.

51 changes: 51 additions & 0 deletions tests/unit/backend/utils/handle-mongo-error.spec.ts
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')
})
})
})

0 comments on commit 0c9e65d

Please sign in to comment.