Skip to content

Commit

Permalink
fix: db client do not throw error when no upload found
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jan 20, 2022
1 parent a14f684 commit 481330c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
7 changes: 6 additions & 1 deletion packages/api/src/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as JWT from './utils/jwt.js'
import { JSONResponse } from './utils/json-response.js'
import { JWT_ISSUER } from './constants.js'
import { HTTPError } from './errors.js'

/**
* @typedef { _id: string, issuer: string } User
Expand Down Expand Up @@ -210,7 +211,11 @@ export async function userUploadsDelete (request, env) {
const user = request.auth.user._id

const res = await env.db.deleteUpload(user, cid)
return new JSONResponse(res)
if (res) {
return new JSONResponse(res)
}

throw new HTTPError('Upload not found', 404)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export class DBClient {
.single()

if (error) {
throw new DBError(error)
return undefined
}

return {
Expand Down
9 changes: 2 additions & 7 deletions packages/db/test/upload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,8 @@ describe('upload', () => {
assert.strictEqual(deletedUpload.updated_at, deletedUpload.deleted_at)

// Should fail to delete again
let error
try {
await client.deleteUpload(user._id, otherCid)
} catch (err) {
error = err
}
assert(error, 'should fail to delete upload again')
const wasDeletedAgain = await client.deleteUpload(user._id, otherCid)
assert.strictEqual(wasDeletedAgain, undefined, 'should fail to delete upload again')

const finalUserUploads = await client.listUploads(user._id)
assert(finalUserUploads, 'user upload deleted')
Expand Down

0 comments on commit 481330c

Please sign in to comment.