Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Merge c3263bb into d582385
Browse files Browse the repository at this point in the history
  • Loading branch information
phoebe-lew authored Nov 22, 2023
2 parents d582385 + c3263bb commit 1a944af
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/omega-carrots-sort.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 0 additions & 5 deletions .changeset/tiny-carrots-sort.md

This file was deleted.

20 changes: 20 additions & 0 deletions packages/http-server/src/request-handlers/callback-error.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions packages/http-server/src/request-handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './callback-error.js'
export * from './get-exchanges.js'
export * from './get-offerings.js'
export * from './submit-close.js'
Expand Down
11 changes: 8 additions & 3 deletions packages/http-server/src/request-handlers/submit-close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'>
Expand Down Expand Up @@ -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)
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] })
}
}
}
}
11 changes: 8 additions & 3 deletions packages/http-server/src/request-handlers/submit-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'>
Expand Down Expand Up @@ -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)
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] })
}
}
}
}
11 changes: 8 additions & 3 deletions packages/http-server/src/request-handlers/submit-rfq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'>
Expand Down Expand Up @@ -53,10 +54,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)
await callback({ request: req, response: res }, message, { offering })
} catch(e) {
// TODO: handle error lewl (#issue 8)
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] })
}
}

return res.sendStatus(202)
Expand Down
12 changes: 11 additions & 1 deletion packages/http-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,21 @@ export type GetCallbacks = {
*/
export type SubmitKind = 'rfq' | 'order' | 'close'

/**
* Types for options provided to {@link SubmitCallback}
* @beta
*/
export type SubmitCallbackOpts = {
'rfq': { offering: Offering }
'order': undefined
'close': undefined
}

/**
* Callback handler for the submit requests
* @beta
*/
export type SubmitCallback<T extends SubmitKind> = (ctx: RequestContext, message: MessageKindClasses[T]) => any
export type SubmitCallback<T extends SubmitKind> = (ctx: RequestContext, message: MessageKindClasses[T], opts: SubmitCallbackOpts[T]) => Promise<void>

/**
* Map of callbacks handlers for the submit requests
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/build/compile-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down

0 comments on commit 1a944af

Please sign in to comment.