From d582385dc11499453d19ddd5358c5d428ec615c1 Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Tue, 21 Nov 2023 18:09:10 -0500 Subject: [PATCH] #63 Restructure error messages as ErrorDetail (#79) * Restructure error messages as ErrorDetail * Type response body of messages * Changeset * Typo --- .changeset/tiny-carrots-sort.md | 5 +++++ .../http-server/src/request-handlers/submit-rfq.ts | 10 ++++++---- packages/http-server/src/types.ts | 3 ++- 3 files changed, 13 insertions(+), 5 deletions(-) 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..c48161c1 --- /dev/null +++ b/.changeset/tiny-carrots-sort.md @@ -0,0 +1,5 @@ +--- +"@tbdex/http-server": patch +--- + +Restructure error messages as ErrorDetail type diff --git a/packages/http-server/src/request-handlers/submit-rfq.ts b/packages/http-server/src/request-handlers/submit-rfq.ts index 8fdecce3..2f75ab53 100644 --- a/packages/http-server/src/request-handlers/submit-rfq.ts +++ b/packages/http-server/src/request-handlers/submit-rfq.ts @@ -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] }) } @@ -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 { @@ -59,4 +61,4 @@ export function submitRfq(options: SubmitRfqOpts): RequestHandler { return res.sendStatus(202) } -} \ No newline at end of file +} diff --git a/packages/http-server/src/types.ts b/packages/http-server/src/types.ts index 5b916d97..75ab35c2 100644 --- a/packages/http-server/src/types.ts +++ b/packages/http-server/src/types.ts @@ -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 @@ -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