From 75514cca074e49e15195a9e4eb5619b61e58d308 Mon Sep 17 00:00:00 2001 From: Phoebe Lew Date: Wed, 22 Nov 2023 18:07:44 +1100 Subject: [PATCH 1/8] Handle http-server callback errors and pass offering to RFQ handler --- .changeset/tiny-carrots-sort.md | 5 ----- packages/http-client/CHANGELOG.md | 6 ++++++ packages/http-client/package.json | 2 +- packages/http-server/CHANGELOG.md | 12 +++++++++++ packages/http-server/package.json | 2 +- .../src/request-handlers/callback-error.ts | 20 +++++++++++++++++++ .../http-server/src/request-handlers/index.ts | 1 + .../src/request-handlers/submit-close.ts | 2 +- .../src/request-handlers/submit-order.ts | 2 +- .../src/request-handlers/submit-rfq.ts | 9 ++++++++- packages/http-server/src/types.ts | 8 +++++++- packages/protocol/CHANGELOG.md | 2 ++ packages/protocol/package.json | 2 +- 13 files changed, 61 insertions(+), 12 deletions(-) delete mode 100644 .changeset/tiny-carrots-sort.md create mode 100644 packages/http-server/src/request-handlers/callback-error.ts diff --git a/.changeset/tiny-carrots-sort.md b/.changeset/tiny-carrots-sort.md deleted file mode 100644 index c48161c1..00000000 --- a/.changeset/tiny-carrots-sort.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@tbdex/http-server": patch ---- - -Restructure error messages as ErrorDetail type diff --git a/packages/http-client/CHANGELOG.md b/packages/http-client/CHANGELOG.md index 41845260..7408632f 100644 --- a/packages/http-client/CHANGELOG.md +++ b/packages/http-client/CHANGELOG.md @@ -1,5 +1,11 @@ # @tbdex/http-client +## 0.19.0 + +### Patch Changes + +- @tbdex/protocol@0.19.0 + ## 0.18.0 ### Minor Changes diff --git a/packages/http-client/package.json b/packages/http-client/package.json index f2383b32..5352512a 100644 --- a/packages/http-client/package.json +++ b/packages/http-client/package.json @@ -1,6 +1,6 @@ { "name": "@tbdex/http-client", - "version": "0.18.0", + "version": "0.19.0", "type": "module", "description": "Http client that can be used to send tbdex messages", "license": "Apache-2.0", diff --git a/packages/http-server/CHANGELOG.md b/packages/http-server/CHANGELOG.md index e6702fe9..e860febf 100644 --- a/packages/http-server/CHANGELOG.md +++ b/packages/http-server/CHANGELOG.md @@ -1,5 +1,17 @@ # @tbdex/http-server +## 0.19.0 + +### Minor Changes + +- Handle callback errors and pass offering to submit RFQ callback + +### Patch Changes + +- d582385: Restructure error messages as ErrorDetail type + - @tbdex/protocol@0.19.0 + - @tbdex/http-client@0.19.0 + ## 0.18.0 ### Minor Changes diff --git a/packages/http-server/package.json b/packages/http-server/package.json index 77436b77..5dcb4531 100644 --- a/packages/http-server/package.json +++ b/packages/http-server/package.json @@ -9,7 +9,7 @@ }, "license": "Apache-2.0", "type": "module", - "version": "0.18.0", + "version": "0.19.0", "module": "./dist/main.js", "types": "./dist/types/main.d.ts", "files": [ diff --git a/packages/http-server/src/request-handlers/callback-error.ts b/packages/http-server/src/request-handlers/callback-error.ts new file mode 100644 index 00000000..94d606b6 --- /dev/null +++ b/packages/http-server/src/request-handlers/callback-error.ts @@ -0,0 +1,20 @@ +import type { ErrorDetail } from '@tbdex/http-client' + +export class CallbackError extends Error { + public readonly statusCode: number + public readonly details: ErrorDetail[] + + constructor(statusCode: number, details: ErrorDetail[] = []) { + super() + + this.name = this.constructor.name + this.statusCode = statusCode + this.details = details + + if (Error.captureStackTrace) { + Error.captureStackTrace(this, this.constructor) + } + + Object.setPrototypeOf(this, CallbackError.prototype) + } +} \ No newline at end of file diff --git a/packages/http-server/src/request-handlers/index.ts b/packages/http-server/src/request-handlers/index.ts index 15092813..d474d9c9 100644 --- a/packages/http-server/src/request-handlers/index.ts +++ b/packages/http-server/src/request-handlers/index.ts @@ -1,3 +1,4 @@ +export * from './callback-error.js' export * from './get-exchanges.js' export * from './get-offerings.js' export * from './submit-close.js' diff --git a/packages/http-server/src/request-handlers/submit-close.ts b/packages/http-server/src/request-handlers/submit-close.ts index 265f329d..e4ab1026 100644 --- a/packages/http-server/src/request-handlers/submit-close.ts +++ b/packages/http-server/src/request-handlers/submit-close.ts @@ -38,7 +38,7 @@ export function submitClose(opts: SubmitCloseOpts): RequestHandler { try { // TODO: figure out what to do with callback result, if anything. (issue #12) - const _result = await callback({ request: req, response: res }, message) + const _result = await callback({ request: req, response: res }, message, undefined) return res.sendStatus(202) } catch(e) { // TODO: handle error lewl (issue #3) diff --git a/packages/http-server/src/request-handlers/submit-order.ts b/packages/http-server/src/request-handlers/submit-order.ts index 37c067c6..759c4846 100644 --- a/packages/http-server/src/request-handlers/submit-order.ts +++ b/packages/http-server/src/request-handlers/submit-order.ts @@ -41,7 +41,7 @@ export function submitOrder(opts: SubmitOrderOpts): RequestHandler { try { // TODO: figure out what to do with callback result, if anything. (#issue 5) - const _result = await callback({ request: req, response: res }, message) + const _result = await callback({ request: req, response: res }, message, undefined) return res.sendStatus(202) } catch(e) { // TODO: handle error lewl diff --git a/packages/http-server/src/request-handlers/submit-rfq.ts b/packages/http-server/src/request-handlers/submit-rfq.ts index 2f75ab53..6c7c0d91 100644 --- a/packages/http-server/src/request-handlers/submit-rfq.ts +++ b/packages/http-server/src/request-handlers/submit-rfq.ts @@ -3,6 +3,7 @@ import type { MessageKind } from '@tbdex/protocol' import type { ErrorDetail } from '@tbdex/http-client' import { Message } from '@tbdex/protocol' +import { CallbackError } from './index.js' type SubmitRfqOpts = { callback: SubmitCallback<'rfq'> @@ -54,8 +55,14 @@ export function submitRfq(options: SubmitRfqOpts): RequestHandler { try { // TODO: figure out what to do with callback result, if anything. (issue #7) - const _result = await callback({ request: req, response: res }, message) + const _result = await callback({ request: req, response: res }, message, { offering }) } catch(e) { + if (e instanceof CallbackError) { + return res.status(e.statusCode).json({ errors: e.details }) + } else { + const errorDetail: ErrorDetail = { detail: 'umm idk' } + return res.status(500).json({ errors: [errorDetail] }) + } // TODO: handle error lewl (#issue 8) } diff --git a/packages/http-server/src/types.ts b/packages/http-server/src/types.ts index 75ab35c2..d720de0d 100644 --- a/packages/http-server/src/types.ts +++ b/packages/http-server/src/types.ts @@ -28,11 +28,17 @@ export type GetCallbacks = { */ export type SubmitKind = 'rfq' | 'order' | 'close' +export type SubmitCallbackOpts = { + 'rfq': { offering: Offering } + 'order': undefined + 'close': undefined +} + /** * Callback handler for the submit requests * @beta */ -export type SubmitCallback = (ctx: RequestContext, message: MessageKindClasses[T]) => any +export type SubmitCallback = (ctx: RequestContext, message: MessageKindClasses[T], opts: SubmitCallbackOpts[T]) => void /** * Map of callbacks handlers for the submit requests diff --git a/packages/protocol/CHANGELOG.md b/packages/protocol/CHANGELOG.md index 6abb14ba..fb0e51b0 100644 --- a/packages/protocol/CHANGELOG.md +++ b/packages/protocol/CHANGELOG.md @@ -1,5 +1,7 @@ # @tbdex/protocol +## 0.19.0 + ## 0.18.0 ## 0.17.0 diff --git a/packages/protocol/package.json b/packages/protocol/package.json index 2c2669ab..93b779bc 100644 --- a/packages/protocol/package.json +++ b/packages/protocol/package.json @@ -1,6 +1,6 @@ { "name": "@tbdex/protocol", - "version": "0.18.0", + "version": "0.19.0", "type": "module", "description": "Library that includes type definitions for tbdex messages", "license": "Apache-2.0", From 15a105ae85f317bf99d09c399a509909ac0b6f3a Mon Sep 17 00:00:00 2001 From: Phoebe Lew Date: Wed, 22 Nov 2023 18:15:33 +1100 Subject: [PATCH 2/8] Additional cleanups --- .../http-server/src/request-handlers/submit-close.ts | 11 ++++++++--- .../http-server/src/request-handlers/submit-order.ts | 11 ++++++++--- .../http-server/src/request-handlers/submit-rfq.ts | 4 +--- packages/http-server/src/types.ts | 2 +- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/http-server/src/request-handlers/submit-close.ts b/packages/http-server/src/request-handlers/submit-close.ts index e4ab1026..ae71292e 100644 --- a/packages/http-server/src/request-handlers/submit-close.ts +++ b/packages/http-server/src/request-handlers/submit-close.ts @@ -3,6 +3,7 @@ import type { ErrorDetail } from '@tbdex/http-client' import type { MessageKind } from '@tbdex/protocol' import { Message } from '@tbdex/protocol' +import { CallbackError } from './index.js' type SubmitCloseOpts = { callback: SubmitCallback<'close'> @@ -37,11 +38,15 @@ export function submitClose(opts: SubmitCloseOpts): RequestHandler { } try { - // TODO: figure out what to do with callback result, if anything. (issue #12) - const _result = await callback({ request: req, response: res }, message, undefined) + await callback({ request: req, response: res }, message, undefined) return res.sendStatus(202) } catch(e) { - // TODO: handle error lewl (issue #3) + if (e instanceof CallbackError) { + return res.status(e.statusCode).json({ errors: e.details }) + } else { + const errorDetail: ErrorDetail = { detail: 'umm idk' } + return res.status(500).json({ errors: [errorDetail] }) + } } } } \ No newline at end of file diff --git a/packages/http-server/src/request-handlers/submit-order.ts b/packages/http-server/src/request-handlers/submit-order.ts index 759c4846..11234dc6 100644 --- a/packages/http-server/src/request-handlers/submit-order.ts +++ b/packages/http-server/src/request-handlers/submit-order.ts @@ -3,6 +3,7 @@ import type { ErrorDetail } from '@tbdex/http-client' import type { MessageKind } from '@tbdex/protocol' import { Message } from '@tbdex/protocol' +import { CallbackError } from './index.js' type SubmitOrderOpts = { callback: SubmitCallback<'order'> @@ -40,11 +41,15 @@ export function submitOrder(opts: SubmitOrderOpts): RequestHandler { } try { - // TODO: figure out what to do with callback result, if anything. (#issue 5) - const _result = await callback({ request: req, response: res }, message, undefined) + await callback({ request: req, response: res }, message, undefined) return res.sendStatus(202) } catch(e) { - // TODO: handle error lewl + if (e instanceof CallbackError) { + return res.status(e.statusCode).json({ errors: e.details }) + } else { + const errorDetail: ErrorDetail = { detail: 'umm idk' } + return res.status(500).json({ errors: [errorDetail] }) + } } } } \ No newline at end of file diff --git a/packages/http-server/src/request-handlers/submit-rfq.ts b/packages/http-server/src/request-handlers/submit-rfq.ts index 6c7c0d91..868bc1c9 100644 --- a/packages/http-server/src/request-handlers/submit-rfq.ts +++ b/packages/http-server/src/request-handlers/submit-rfq.ts @@ -54,8 +54,7 @@ export function submitRfq(options: SubmitRfqOpts): RequestHandler { } try { - // TODO: figure out what to do with callback result, if anything. (issue #7) - const _result = await callback({ request: req, response: res }, message, { offering }) + await callback({ request: req, response: res }, message, { offering }) } catch(e) { if (e instanceof CallbackError) { return res.status(e.statusCode).json({ errors: e.details }) @@ -63,7 +62,6 @@ export function submitRfq(options: SubmitRfqOpts): RequestHandler { const errorDetail: ErrorDetail = { detail: 'umm idk' } return res.status(500).json({ errors: [errorDetail] }) } - // TODO: handle error lewl (#issue 8) } return res.sendStatus(202) diff --git a/packages/http-server/src/types.ts b/packages/http-server/src/types.ts index d720de0d..4418c31c 100644 --- a/packages/http-server/src/types.ts +++ b/packages/http-server/src/types.ts @@ -38,7 +38,7 @@ export type SubmitCallbackOpts = { * Callback handler for the submit requests * @beta */ -export type SubmitCallback = (ctx: RequestContext, message: MessageKindClasses[T], opts: SubmitCallbackOpts[T]) => void +export type SubmitCallback = (ctx: RequestContext, message: MessageKindClasses[T], opts: SubmitCallbackOpts[T]) => Promise /** * Map of callbacks handlers for the submit requests From c5aa40bf27c08356a2463a02631c38759557fc13 Mon Sep 17 00:00:00 2001 From: Phoebe Lew Date: Wed, 22 Nov 2023 18:18:03 +1100 Subject: [PATCH 3/8] Add changeset back --- .changeset/tiny-carrots-sort.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/tiny-carrots-sort.md diff --git a/.changeset/tiny-carrots-sort.md b/.changeset/tiny-carrots-sort.md new file mode 100644 index 00000000..dae12525 --- /dev/null +++ b/.changeset/tiny-carrots-sort.md @@ -0,0 +1,6 @@ +--- +"@tbdex/http-server": minor +--- + +Restructure error messages as ErrorDetail type +Handle http-server callback errors and pass offering to RFQ handler \ No newline at end of file From ff610bde47fa6ff48f316d04820efbd4d7fbf813 Mon Sep 17 00:00:00 2001 From: Phoebe Lew Date: Wed, 22 Nov 2023 18:22:11 +1100 Subject: [PATCH 4/8] undo version bumps --- packages/http-client/CHANGELOG.md | 6 ------ packages/http-client/package.json | 2 +- packages/http-server/CHANGELOG.md | 12 ------------ packages/http-server/package.json | 2 +- packages/protocol/CHANGELOG.md | 2 -- packages/protocol/package.json | 2 +- 6 files changed, 3 insertions(+), 23 deletions(-) diff --git a/packages/http-client/CHANGELOG.md b/packages/http-client/CHANGELOG.md index 7408632f..41845260 100644 --- a/packages/http-client/CHANGELOG.md +++ b/packages/http-client/CHANGELOG.md @@ -1,11 +1,5 @@ # @tbdex/http-client -## 0.19.0 - -### Patch Changes - -- @tbdex/protocol@0.19.0 - ## 0.18.0 ### Minor Changes diff --git a/packages/http-client/package.json b/packages/http-client/package.json index 5352512a..f2383b32 100644 --- a/packages/http-client/package.json +++ b/packages/http-client/package.json @@ -1,6 +1,6 @@ { "name": "@tbdex/http-client", - "version": "0.19.0", + "version": "0.18.0", "type": "module", "description": "Http client that can be used to send tbdex messages", "license": "Apache-2.0", diff --git a/packages/http-server/CHANGELOG.md b/packages/http-server/CHANGELOG.md index e860febf..e6702fe9 100644 --- a/packages/http-server/CHANGELOG.md +++ b/packages/http-server/CHANGELOG.md @@ -1,17 +1,5 @@ # @tbdex/http-server -## 0.19.0 - -### Minor Changes - -- Handle callback errors and pass offering to submit RFQ callback - -### Patch Changes - -- d582385: Restructure error messages as ErrorDetail type - - @tbdex/protocol@0.19.0 - - @tbdex/http-client@0.19.0 - ## 0.18.0 ### Minor Changes diff --git a/packages/http-server/package.json b/packages/http-server/package.json index 5dcb4531..77436b77 100644 --- a/packages/http-server/package.json +++ b/packages/http-server/package.json @@ -9,7 +9,7 @@ }, "license": "Apache-2.0", "type": "module", - "version": "0.19.0", + "version": "0.18.0", "module": "./dist/main.js", "types": "./dist/types/main.d.ts", "files": [ diff --git a/packages/protocol/CHANGELOG.md b/packages/protocol/CHANGELOG.md index fb0e51b0..6abb14ba 100644 --- a/packages/protocol/CHANGELOG.md +++ b/packages/protocol/CHANGELOG.md @@ -1,7 +1,5 @@ # @tbdex/protocol -## 0.19.0 - ## 0.18.0 ## 0.17.0 diff --git a/packages/protocol/package.json b/packages/protocol/package.json index 93b779bc..2c2669ab 100644 --- a/packages/protocol/package.json +++ b/packages/protocol/package.json @@ -1,6 +1,6 @@ { "name": "@tbdex/protocol", - "version": "0.19.0", + "version": "0.18.0", "type": "module", "description": "Library that includes type definitions for tbdex messages", "license": "Apache-2.0", From 9f1e419a03a3f48daed7dd7fb82315e886e5882f Mon Sep 17 00:00:00 2001 From: Phoebe Lew Date: Wed, 22 Nov 2023 18:24:23 +1100 Subject: [PATCH 5/8] add changeset file --- .changeset/{tiny-carrots-sort.md => omega-carrots-sort.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .changeset/{tiny-carrots-sort.md => omega-carrots-sort.md} (100%) diff --git a/.changeset/tiny-carrots-sort.md b/.changeset/omega-carrots-sort.md similarity index 100% rename from .changeset/tiny-carrots-sort.md rename to .changeset/omega-carrots-sort.md From 1c2ed4fcaf64d3fdc8c2ec19c41bb3fc76c3ea48 Mon Sep 17 00:00:00 2001 From: Phoebe Lew Date: Wed, 22 Nov 2023 18:29:03 +1100 Subject: [PATCH 6/8] Fix schema URL --- packages/protocol/build/compile-validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/protocol/build/compile-validators.js b/packages/protocol/build/compile-validators.js index db30ad87..6194831f 100644 --- a/packages/protocol/build/compile-validators.js +++ b/packages/protocol/build/compile-validators.js @@ -17,7 +17,7 @@ import standaloneCode from 'ajv/dist/standalone/index.js' import { mkdirp } from 'mkdirp' -const schemaHostUrl = 'https://tbdex.dev' +const schemaHostUrl = 'https://tbdex.dev/json-schemas' const schemaUrls = { definitions : `${schemaHostUrl}/definitions.json`, resource : `${schemaHostUrl}/resource.schema.json`, From 72286e9cb1e3b3c518ba280833d3dbc9e8c66c90 Mon Sep 17 00:00:00 2001 From: Moe Jangda Date: Wed, 22 Nov 2023 01:35:37 -0600 Subject: [PATCH 7/8] address tbdocs report --- .changeset/omega-carrots-sort.md | 4 ++-- packages/http-server/src/types.ts | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.changeset/omega-carrots-sort.md b/.changeset/omega-carrots-sort.md index dae12525..b14c6701 100644 --- a/.changeset/omega-carrots-sort.md +++ b/.changeset/omega-carrots-sort.md @@ -2,5 +2,5 @@ "@tbdex/http-server": minor --- -Restructure error messages as ErrorDetail type -Handle http-server callback errors and pass offering to RFQ handler \ No newline at end of file +* Restructure error messages as ErrorDetail type +* Handle http-server callback errors and pass offering to RFQ handler diff --git a/packages/http-server/src/types.ts b/packages/http-server/src/types.ts index 4418c31c..86f8681a 100644 --- a/packages/http-server/src/types.ts +++ b/packages/http-server/src/types.ts @@ -28,6 +28,9 @@ export type GetCallbacks = { */ export type SubmitKind = 'rfq' | 'order' | 'close' +/** + * Types for options provided to {@link SubmitCallback} + */ export type SubmitCallbackOpts = { 'rfq': { offering: Offering } 'order': undefined From c3263bb01d48ab5046199f1d80c2d86df2cbae65 Mon Sep 17 00:00:00 2001 From: Moe Jangda Date: Wed, 22 Nov 2023 01:37:43 -0600 Subject: [PATCH 8/8] @beta fish --- packages/http-server/src/types.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/http-server/src/types.ts b/packages/http-server/src/types.ts index 86f8681a..5cf06aa8 100644 --- a/packages/http-server/src/types.ts +++ b/packages/http-server/src/types.ts @@ -30,6 +30,7 @@ export type SubmitKind = 'rfq' | 'order' | 'close' /** * Types for options provided to {@link SubmitCallback} + * @beta */ export type SubmitCallbackOpts = { 'rfq': { offering: Offering }