From c8336359d4c847026bc415b052c2fcdfd5a791cf Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Fri, 4 Aug 2023 14:08:41 +0200 Subject: [PATCH] chore: simplify errors --- packages/capabilities/src/types.ts | 18 +++++++++-- packages/filecoin-api/package.json | 10 ++++++- packages/filecoin-api/src/aggregator.js | 4 +-- packages/filecoin-api/src/broker.js | 11 ++++--- packages/filecoin-api/src/errors.js | 30 +++++++++++++------ packages/filecoin-api/src/storefront.js | 4 +-- .../filecoin-api/test/services/aggregator.js | 10 +++++-- packages/filecoin-api/test/services/broker.js | 7 +++-- .../filecoin-api/test/services/storefront.js | 7 +++-- pnpm-lock.yaml | 11 ++++++- 10 files changed, 79 insertions(+), 33 deletions(-) diff --git a/packages/capabilities/src/types.ts b/packages/capabilities/src/types.ts index e373d0e83..c3fad7d8f 100644 --- a/packages/capabilities/src/types.ts +++ b/packages/capabilities/src/types.ts @@ -83,7 +83,6 @@ export interface FilecoinAddSuccess { } export interface FilecoinAddFailure extends Ucanto.Failure { reason: string - piece: PieceLink } export interface PieceAddSuccess { @@ -93,7 +92,6 @@ export interface PieceAddSuccess { } export interface PieceAddFailure extends Ucanto.Failure { reason: string - piece: PieceLink } export interface AggregateAddSuccess { @@ -101,7 +99,21 @@ export interface AggregateAddSuccess { piece?: PieceLink } -export interface AggregateAddFailure extends Ucanto.Failure {} +export type AggregateAddFailure = AggregateAddParseFailure | AggregateAddFailureWithBadPiece + +export interface AggregateAddParseFailure extends Ucanto.Failure { + reason: string +} + +export interface AggregateAddFailureWithBadPiece extends Ucanto.Failure { + piece?: PieceLink + cause?: AggregateAddFailureCause[] | unknown +} + +export interface AggregateAddFailureCause { + piece: PieceLink + reason: string +} // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface ChainInfoSuccess { diff --git a/packages/filecoin-api/package.json b/packages/filecoin-api/package.json index 5bbafcc34..3d0b88b68 100644 --- a/packages/filecoin-api/package.json +++ b/packages/filecoin-api/package.json @@ -23,6 +23,9 @@ "chain": [ "dist/src/chain.d.ts" ], + "errors": [ + "dist/src/errors.d.ts" + ], "storefront": [ "dist/src/storefront.d.ts" ], @@ -59,6 +62,10 @@ "types": "./dist/src/storefront.d.ts", "import": "./src/storefront.js" }, + "./errors": { + "types": "./dist/src/errors.d.ts", + "import": "./src/errors.js" + }, "./test": { "types": "./dist/test/lib.d.ts", "import": "./test/lib.js" @@ -90,7 +97,8 @@ "@web3-storage/filecoin-client": "workspace:^", "hd-scripts": "^4.1.0", "mocha": "^10.2.0", - "multiformats": "^11.0.2" + "multiformats": "^11.0.2", + "p-wait-for": "^5.0.2" }, "eslintConfig": { "extends": [ diff --git a/packages/filecoin-api/src/aggregator.js b/packages/filecoin-api/src/aggregator.js index 2e6d830a8..8f4a3888d 100644 --- a/packages/filecoin-api/src/aggregator.js +++ b/packages/filecoin-api/src/aggregator.js @@ -36,7 +36,7 @@ async function queueAdd(piece, storefront, group, context) { }) if (queued.error) { return { - error: new QueueOperationFailed(queued.error.message, piece), + error: new QueueOperationFailed(queued.error.message), } } @@ -76,7 +76,7 @@ async function queueHandler(piece, storefront, group, context) { if (put.error) { return { - error: new StoreOperationFailed(put.error.message, piece), + error: new StoreOperationFailed(put.error.message), } } diff --git a/packages/filecoin-api/src/broker.js b/packages/filecoin-api/src/broker.js index 7a7f6ab23..faa7a0b06 100644 --- a/packages/filecoin-api/src/broker.js +++ b/packages/filecoin-api/src/broker.js @@ -23,8 +23,7 @@ export const claim = async ({ capability, invocation }, context) => { if (!offer) { return { error: new DecodeBlockOperationFailed( - `missing offer block in invocation: ${offerCid.toString()}`, - piece + `missing offer block in invocation: ${offerCid.toString()}` ), } } @@ -53,7 +52,7 @@ async function queueAdd(piece, offerCid, deal, offer, context) { }) if (queued.error) { return { - error: new QueueOperationFailed(queued.error.message, piece), + error: new QueueOperationFailed(queued.error.message), } } @@ -85,6 +84,8 @@ async function queueAdd(piece, offerCid, deal, offer, context) { * @returns {Promise | API.UcantoInterface.JoinBuilder>} */ async function queueHandler(piece, offer, deal, context) { + // TODO: failure - needs to read from store + const put = await context.offerStore.put({ offer, piece, @@ -92,12 +93,10 @@ async function queueHandler(piece, offer, deal, context) { }) if (put.error) { return { - error: new StoreOperationFailed(put.error.message, piece), + error: new StoreOperationFailed(put.error.message), } } - // TODO: failure - return { ok: { status: 'accepted', diff --git a/packages/filecoin-api/src/errors.js b/packages/filecoin-api/src/errors.js index 9f616801c..bdcb7b656 100644 --- a/packages/filecoin-api/src/errors.js +++ b/packages/filecoin-api/src/errors.js @@ -6,11 +6,9 @@ export const QueueOperationErrorName = /** @type {const} */ ( export class QueueOperationFailed extends Server.Failure { /** * @param {string} message - * @param {import('@web3-storage/data-segment').PieceLink} piece */ - constructor(message, piece) { + constructor(message) { super(message) - this.piece = piece } get reason() { return this.message @@ -26,11 +24,9 @@ export const StoreOperationErrorName = /** @type {const} */ ( export class StoreOperationFailed extends Server.Failure { /** * @param {string} message - * @param {import('@web3-storage/data-segment').PieceLink} piece */ - constructor(message, piece) { + constructor(message) { super(message) - this.piece = piece } get reason() { return this.message @@ -40,17 +36,33 @@ export class StoreOperationFailed extends Server.Failure { } } +export const StoreNotFoundErrorName = /** @type {const} */ ( + 'StoreNotFound' +) +export class StoreNotFound extends Server.Failure { + /** + * @param {string} message + */ + constructor(message) { + super(message) + } + get reason() { + return this.message + } + get name() { + return StoreNotFoundErrorName + } +} + export const DecodeBlockOperationErrorName = /** @type {const} */ ( 'DecodeBlockOperationFailed' ) export class DecodeBlockOperationFailed extends Server.Failure { /** * @param {string} message - * @param {import('@web3-storage/data-segment').PieceLink} piece */ - constructor(message, piece) { + constructor(message) { super(message) - this.piece = piece } get reason() { return this.message diff --git a/packages/filecoin-api/src/storefront.js b/packages/filecoin-api/src/storefront.js index 249e9b217..708dd8bc1 100644 --- a/packages/filecoin-api/src/storefront.js +++ b/packages/filecoin-api/src/storefront.js @@ -38,7 +38,7 @@ async function queueAdd(piece, content, context) { }) if (queued.error) { return { - error: new QueueOperationFailed(queued.error.message, piece), + error: new QueueOperationFailed(queued.error.message), } } @@ -75,7 +75,7 @@ async function queueHandler(piece, content, context) { }) if (put.error) { return { - error: new StoreOperationFailed(put.error.message, piece), + error: new StoreOperationFailed(put.error.message), } } diff --git a/packages/filecoin-api/test/services/aggregator.js b/packages/filecoin-api/test/services/aggregator.js index cd883477d..03fba22db 100644 --- a/packages/filecoin-api/test/services/aggregator.js +++ b/packages/filecoin-api/test/services/aggregator.js @@ -1,5 +1,6 @@ import { Filecoin } from '@web3-storage/capabilities' import * as Signer from '@ucanto/principal/ed25519' +import pWaitFor from 'p-wait-for' import * as API from '../../src/types.js' @@ -58,7 +59,7 @@ export const test = { assert.ok(fx.link().equals(response.fx.join?.link())) // Validate queue and store - assert.ok(context.queuedMessages.length === 1) + await pWaitFor(() => context.queuedMessages.length === 1) const hasStoredPiece = await context.pieceStore.get({ piece: cargo.link.link(), @@ -100,13 +101,16 @@ export const test = { assert.deepEqual(response.out.ok.status, 'accepted') // Validate queue and store - assert.ok(context.queuedMessages.length === 0) + await pWaitFor(() => context.queuedMessages.length === 0) const hasStoredPiece = await context.pieceStore.get({ piece: cargo.link.link(), storefront: storefront.did(), }) assert.ok(hasStoredPiece.ok) + assert.ok(hasStoredPiece.ok?.piece.equals(cargo.link.link())) + assert.deepEqual(hasStoredPiece.ok?.group, group) + assert.deepEqual(hasStoredPiece.ok?.storefront, storefront.did()) }, 'skip piece/add from signer inserts piece into store and returns rejected': async (assert, context) => { @@ -140,7 +144,7 @@ export const test = { assert.deepEqual(response.out.ok.status, 'rejected') // Validate queue and store - assert.ok(context.queuedMessages.length === 0) + await pWaitFor(() => context.queuedMessages.length === 0) const hasStoredPiece = await context.pieceStore.get({ piece: cargo.link.link(), diff --git a/packages/filecoin-api/test/services/broker.js b/packages/filecoin-api/test/services/broker.js index d935cf7dd..68a320995 100644 --- a/packages/filecoin-api/test/services/broker.js +++ b/packages/filecoin-api/test/services/broker.js @@ -1,5 +1,6 @@ import { Filecoin } from '@web3-storage/capabilities' import * as Signer from '@ucanto/principal/ed25519' +import pWaitFor from 'p-wait-for' import { CBOR } from '@ucanto/core' import * as API from '../../src/types.js' @@ -67,7 +68,7 @@ export const test = { assert.ok(fx.link().equals(response.fx.join?.link())) // Validate queue and store - assert.ok(context.queuedMessages.length === 1) + await pWaitFor(() => context.queuedMessages.length === 1) const hasStoredOffer = await context.offerStore.get({ piece: aggregate.link.link(), @@ -110,7 +111,7 @@ export const test = { assert.deepEqual(response.out.ok.status, 'accepted') // Validate queue and store - assert.ok(context.queuedMessages.length === 0) + await pWaitFor(() => context.queuedMessages.length === 0) const hasStoredOffer = await context.offerStore.get({ piece: aggregate.link.link(), @@ -153,7 +154,7 @@ export const test = { assert.deepEqual(response.out.ok.status, 'rejected') // Validate queue and store - assert.ok(context.queuedMessages.length === 0) + await pWaitFor(() => context.queuedMessages.length === 0) const hasStoredOffer = await context.offerStore.get({ piece: aggregate.link.link(), diff --git a/packages/filecoin-api/test/services/storefront.js b/packages/filecoin-api/test/services/storefront.js index c8d479401..ba8d3c17d 100644 --- a/packages/filecoin-api/test/services/storefront.js +++ b/packages/filecoin-api/test/services/storefront.js @@ -1,5 +1,6 @@ import { Filecoin } from '@web3-storage/capabilities' import * as Signer from '@ucanto/principal/ed25519' +import pWaitFor from 'p-wait-for' import * as API from '../../src/types.js' @@ -58,7 +59,7 @@ export const test = { assert.ok(fx.link().equals(response.fx.join?.link())) // Validate queue and store - assert.ok(context.queuedMessages.length === 1) + await pWaitFor(() => context.queuedMessages.length === 1) const hasStoredPiece = await context.pieceStore.get({ piece: cargo.link.link(), @@ -94,7 +95,7 @@ export const test = { assert.deepEqual(response.out.ok.status, 'accepted') // Validate queue and store - assert.ok(context.queuedMessages.length === 0) + await pWaitFor(() => context.queuedMessages.length === 0) const hasStoredPiece = await context.pieceStore.get({ piece: cargo.link.link(), @@ -130,7 +131,7 @@ export const test = { assert.deepEqual(response.out.ok.status, 'rejected') // Validate queue and store - assert.ok(context.queuedMessages.length === 0) + await pWaitFor(() => context.queuedMessages.length === 0) const hasStoredPiece = await context.pieceStore.get({ piece: cargo.link.link(), diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3c8f7ad84..18f11f96b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -279,6 +279,9 @@ importers: multiformats: specifier: ^11.0.2 version: 11.0.2 + p-wait-for: + specifier: ^5.0.2 + version: 5.0.2 packages/filecoin-client: dependencies: @@ -8811,7 +8814,6 @@ packages: /p-timeout@6.1.2: resolution: {integrity: sha512-UbD77BuZ9Bc9aABo74gfXhNvzC9Tx7SxtHSh1fxvx3jTLLYvmVhiQZZrJzqqU0jKbN32kb5VOKiLEQI/3bIjgQ==} engines: {node: '>=14.16'} - dev: false /p-try@2.2.0: resolution: {integrity: sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==} @@ -8831,6 +8833,13 @@ packages: p-timeout: 6.1.2 dev: false + /p-wait-for@5.0.2: + resolution: {integrity: sha512-lwx6u1CotQYPVju77R+D0vFomni/AqRfqLmqQ8hekklqZ6gAY9rONh7lBQ0uxWMkC2AuX9b2DVAl8To0NyP1JA==} + engines: {node: '>=12'} + dependencies: + p-timeout: 6.1.2 + dev: true + /package-json@6.5.0: resolution: {integrity: sha512-k3bdm2n25tkyxcjSKzB5x8kfVxlMdgsbPr0GkZcwHsLpba6cBjqCt1KlcChKEvxHIcTB1FVMuwoijZ26xex5MQ==} engines: {node: '>=8'}