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

#63 Restructure error messages as ErrorDetail #79

Merged
merged 4 commits into from
Nov 21, 2023
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
5 changes: 5 additions & 0 deletions .changeset/tiny-carrots-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tbdex/http-server": patch
---

Restructure error messages as ErrorDetail type
10 changes: 6 additions & 4 deletions packages/http-server/src/request-handlers/submit-rfq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function submitRfq(options: SubmitRfqOpts): RequestHandler {
try {
message = await Message.parse(req.body)
} catch(e) {
const errorResponse: ErrorDetail = { detail: e.message }
const errorResponse: ErrorDetail = { detail: `Parsing of TBDex message failed: ${e.message}` }
return res.status(400).json({ errors: [errorResponse] })
}

Expand All @@ -31,12 +31,14 @@ export function submitRfq(options: SubmitRfqOpts): RequestHandler {

const rfqExists = !! await exchangesApi.getRfq({ exchangeId: message.id })
if (rfqExists) {
return res.status(409).json({ errors: [`rfq ${message.id} already exists`] })
const errorResponse: ErrorDetail = { detail: `rfq ${message.id} already exists`}
return res.status(409).json({ errors: [errorResponse] })
}

const offering = await offeringsApi.getOffering({ id: message.data.offeringId })
if (!offering) {
return res.status(400).json({ errors: [`offering ${message.data.offeringId} does not exist`] })
const errorResponse: ErrorDetail = { detail: `offering ${message.data.offeringId} does not exist` }
return res.status(400).json({ errors: [errorResponse] })
}

try {
Expand All @@ -59,4 +61,4 @@ export function submitRfq(options: SubmitRfqOpts): RequestHandler {

return res.sendStatus(202)
}
}
}
3 changes: 2 additions & 1 deletion packages/http-server/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Request, Response } from 'express'
import type { Close, MessageKindClass, MessageKindClasses, Offering, Order, OrderStatus, Quote, Rfq } from '@tbdex/protocol'
import type { ErrorDetail } from '@tbdex/http-client'

/**
* Union type for get requests
Expand Down Expand Up @@ -89,7 +90,7 @@ export type RequestContext = {
* Type alias for the request handler
* @beta
*/
export type RequestHandler = (request: Request, response: Response) => any
export type RequestHandler = (request: Request, response: Response<{ errors?: ErrorDetail[], data?: any }>) => any

/**
* PFI Offerings API
Expand Down
Loading