Skip to content

Commit

Permalink
chore: simplify errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Aug 4, 2023
1 parent 8e34375 commit c833635
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 33 deletions.
18 changes: 15 additions & 3 deletions packages/capabilities/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export interface FilecoinAddSuccess {
}
export interface FilecoinAddFailure extends Ucanto.Failure {
reason: string
piece: PieceLink
}

export interface PieceAddSuccess {
Expand All @@ -93,15 +92,28 @@ export interface PieceAddSuccess {
}
export interface PieceAddFailure extends Ucanto.Failure {
reason: string
piece: PieceLink
}

export interface AggregateAddSuccess {
status: QUEUE_STATUS
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 {
Expand Down
10 changes: 9 additions & 1 deletion packages/filecoin-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"chain": [
"dist/src/chain.d.ts"
],
"errors": [
"dist/src/errors.d.ts"
],
"storefront": [
"dist/src/storefront.d.ts"
],
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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": [
Expand Down
4 changes: 2 additions & 2 deletions packages/filecoin-api/src/aggregator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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),
}
}

Expand Down
11 changes: 5 additions & 6 deletions packages/filecoin-api/src/broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`
),
}
}
Expand Down Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -85,19 +84,19 @@ async function queueAdd(piece, offerCid, deal, offer, context) {
* @returns {Promise<API.UcantoInterface.Result<API.AggregateAddSuccess, API.AggregateAddFailure> | API.UcantoInterface.JoinBuilder<API.AggregateAddSuccess>>}
*/
async function queueHandler(piece, offer, deal, context) {
// TODO: failure - needs to read from store

const put = await context.offerStore.put({
offer,
piece,
deal,
})
if (put.error) {
return {
error: new StoreOperationFailed(put.error.message, piece),
error: new StoreOperationFailed(put.error.message),
}
}

// TODO: failure

return {
ok: {
status: 'accepted',
Expand Down
30 changes: 21 additions & 9 deletions packages/filecoin-api/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/filecoin-api/src/storefront.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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),
}
}

Expand Down
10 changes: 7 additions & 3 deletions packages/filecoin-api/test/services/aggregator.js
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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(),
Expand Down
7 changes: 4 additions & 3 deletions packages/filecoin-api/test/services/broker.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
7 changes: 4 additions & 3 deletions packages/filecoin-api/test/services/storefront.js
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
11 changes: 10 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c833635

Please sign in to comment.