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

fix: db client do not throw error when no upload found #885

Merged
Merged
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
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 @@ -263,8 +263,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 @@ -277,6 +277,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