From 834d3ccb22d8ee00b4f47e5eae850aaf5b7ebba1 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Mon, 7 Feb 2022 14:53:47 +0000 Subject: [PATCH] fix: db client do not throw error when no upload found (#885) --- packages/api/src/user.js | 7 ++++++- packages/db/index.js | 8 ++++++-- packages/db/test/upload.spec.js | 9 ++------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/api/src/user.js b/packages/api/src/user.js index 0dabd57a81..aec7a69008 100644 --- a/packages/api/src/user.js +++ b/packages/api/src/user.js @@ -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 @@ -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) } /** diff --git a/packages/db/index.js b/packages/db/index.js index 552b83c507..80c884460a 100644 --- a/packages/db/index.js +++ b/packages/db/index.js @@ -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, @@ -338,6 +338,10 @@ export class DBClient { .is('deleted_at', null) .single() + if (status === 406 || !data) { + return + } + if (error) { throw new DBError(error) } diff --git a/packages/db/test/upload.spec.js b/packages/db/test/upload.spec.js index c77802fbfd..acfa761fed 100644 --- a/packages/db/test/upload.spec.js +++ b/packages/db/test/upload.spec.js @@ -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')