Skip to content

Commit

Permalink
fix: db client do not throw error when no upload found (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Feb 7, 2022
1 parent cef6788 commit 834d3cc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 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
8 changes: 6 additions & 2 deletions packages/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ export class DBClient {
*/
async deleteUpload (userId, cid) {
const now = new Date().toISOString()
/** @type {{ data: import('./db-client-types').UploadItem, error: PostgrestError }} */
const { data, error } = await this._client
/** @type {{ data: import('./db-client-types').UploadItem, error: PostgrestError, status: number }} */
const { data, error, status } = await this._client
.from('upload')
.update({
deleted_at: now,
Expand All @@ -338,6 +338,10 @@ export class DBClient {
.is('deleted_at', null)
.single()

if (status === 406 || !data) {
return
}

if (error) {
throw new DBError(error)
}
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 834d3cc

Please sign in to comment.