[SafeJsonParse] migrate to OError and use a new DataTooLargeToParseError

This commit is contained in:
Jakob Ackermann 2020-08-20 11:03:34 +01:00
parent f82177a46a
commit 5950b26a42
3 changed files with 13 additions and 8 deletions

View file

@ -6,4 +6,13 @@ class CodedError extends OError {
}
}
module.exports = { CodedError }
class DataTooLargeToParseError extends OError {
constructor(data) {
super('data too large to parse', {
head: data.slice(0, 1024),
length: data.length
})
}
}
module.exports = { CodedError, DataTooLargeToParseError }

View file

@ -1,14 +1,10 @@
const Settings = require('settings-sharelatex')
const logger = require('logger-sharelatex')
const { DataTooLargeToParseError } = require('./Errors')
module.exports = {
parse(data, callback) {
if (data.length > Settings.maxUpdateSize) {
logger.error(
{ head: data.slice(0, 1024), length: data.length },
'data too large to parse'
)
return callback(new Error('data too large to parse'))
return callback(new DataTooLargeToParseError(data))
}
let parsed
try {

View file

@ -50,7 +50,7 @@ describe('SafeJsonParse', function () {
const data = `{\"foo\": \"${big_blob}\"}`
this.Settings.maxUpdateSize = 2 * 1024
return this.SafeJsonParse.parse(data, (error, parsed) => {
this.logger.error.called.should.equal(true)
this.logger.error.called.should.equal(false)
expect(error).to.exist
return done()
})