Skip to content
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

chore(api/diff): improve error message when json parsing objects #3536

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/server/modules/core/rest/diffDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { validatePermissionsReadStreamFactory } from '@/modules/core/services/st
import { getStreamFactory } from '@/modules/core/repositories/streams'
import { authorizeResolver, validateScopes } from '@/modules/shared'
import { getProjectDbClient } from '@/modules/multiregion/dbSelector'
import { UserInputError } from '@/modules/core/errors/userinput'
import { ensureError } from '@speckle/shared'

export default (app: Application) => {
const validatePermissionsReadStream = validatePermissionsReadStreamFactory({
Expand All @@ -34,7 +36,15 @@ export default (app: Application) => {

const projectDb = await getProjectDbClient({ projectId: req.params.streamId })
const getObjectsStream = getObjectsStreamFactory({ db: projectDb })
const childrenList = JSON.parse(req.body.objects)
let childrenList: string[] = []
try {
childrenList = JSON.parse(req.body.objects)
} catch (err) {
throw new UserInputError(
'Error parsing the objects parameter. The objects parameter value should be a string. The contents of the string is expected to be a stringified JSON array of object ids.',
ensureError(err, 'Unknown JSON parse error.')
)
}
const simpleText = req.headers.accept === 'text/plain'

res.writeHead(200, {
Expand Down
13 changes: 11 additions & 2 deletions packages/server/modules/core/rest/diffUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { hasObjectsFactory } from '@/modules/core/repositories/objects'
import { validatePermissionsWriteStreamFactory } from '@/modules/core/services/streams/auth'
import { authorizeResolver, validateScopes } from '@/modules/shared'
import { getProjectDbClient } from '@/modules/multiregion/dbSelector'
import { UserInputError } from '@/modules/core/errors/userinput'
import { ensureError } from '@speckle/shared'

export default (app: Application) => {
const validatePermissionsWriteStream = validatePermissionsWriteStreamFactory({
Expand All @@ -30,8 +32,15 @@ export default (app: Application) => {

const projectDb = await getProjectDbClient({ projectId: req.params.streamId })
const hasObjects = hasObjectsFactory({ db: projectDb })
const objectList = JSON.parse(req.body.objects)

let objectList: string[] = []
try {
objectList = JSON.parse(req.body.objects)
} catch (err) {
throw new UserInputError(
'Error parsing the objects parameter. The objects parameter value should be a string. The contents of the string is expected to be a stringified JSON array of object ids.',
ensureError(err, 'Unknown JSON parse error.')
)
}
req.log.info({ objectCount: objectList.length }, 'Diffing {objectCount} objects.')

const chunkSize = 1000
Expand Down
8 changes: 4 additions & 4 deletions packages/server/modules/core/tests/apitokens.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ describe('API Tokens', () => {
.set('Authorization', `Bearer ${limitedToken1}`)
.send({ fake: 'data' })

// We sent an invalid payload so 500 is fine, as long as its not a 401
expect(resAllowed).to.have.status(500)
// We sent an invalid payload so 400 is expected, as long as its not a 401
expect(resAllowed).to.have.status(400)

const resDisallowed = await request(app)
.post(`/api/getobjects/${stream2.id}`)
Expand All @@ -542,8 +542,8 @@ describe('API Tokens', () => {
.set('Authorization', `Bearer ${limitedToken1}`)
.send({ fake: 'data' })

// We sent an invalid payload so 500 is fine, as long as its not a 401
expect(resAllowed).to.have.status(500)
// We sent an invalid payload so 400 is fine, as long as its not a 401
expect(resAllowed).to.have.status(400)

const resDisallowed = await request(app)
.post(`/api/diff/${stream2.id}`)
Expand Down