diff --git a/app/api/__generated__/msw-handlers.ts b/app/api/__generated__/msw-handlers.ts index 9f698776e6..a91f95b231 100644 --- a/app/api/__generated__/msw-handlers.ts +++ b/app/api/__generated__/msw-handlers.ts @@ -1341,9 +1341,10 @@ function validateParams( // if any of the errors come from path params, just 404 — the resource cannot // exist if there's no valid name - const { issues } = result.error - const status = issues.some((e) => e.path[0] === 'path') ? 404 : 400 - return { paramsErr: json(issues, { status }) } + const status = result.error.issues.some((e) => e.path[0] === 'path') ? 404 : 400 + const error_code = status === 404 ? 'NotFound' : 'InvalidRequest' + const message = 'Zod error for params: ' + JSON.stringify(result.error) + return { paramsErr: json({ error_code, message }, { status }) } } const handler = @@ -1364,7 +1365,7 @@ const handler = const { params, paramsErr } = paramSchema ? validateParams(paramSchema, req, pathParams) : { params: {}, paramsErr: undefined } - if (paramsErr) return json(paramsErr, { status: 400 }) + if (paramsErr) return paramsErr const { path, query } = params @@ -1372,7 +1373,10 @@ const handler = if (bodySchema) { const rawBody = await req.json() const result = bodySchema.transform(snakeify).safeParse(rawBody) - if (!result.success) return json(result.error.issues, { status: 400 }) + if (!result.success) { + const message = 'Zod error for body: ' + JSON.stringify(result.error) + return json({ error_code: 'InvalidRequest', message }, { status: 400 }) + } body = result.data } @@ -1387,9 +1391,6 @@ const handler = if (typeof result === 'number') { return new HttpResponse(null, { status: result }) } - if (typeof result === 'function') { - return result() - } if (result instanceof Response) { return result } @@ -1398,17 +1399,22 @@ const handler = if (typeof thrown === 'number') { return new HttpResponse(null, { status: thrown }) } - if (typeof thrown === 'function') { - return thrown() - } if (typeof thrown === 'string') { return json({ message: thrown }, { status: 400 }) } if (thrown instanceof Response) { return thrown } + + // if it's not one of those, then we don't know what to do with it console.error('Unexpected mock error', thrown) - return json({ message: 'Unknown Server Error' }, { status: 500 }) + if (typeof thrown === 'function') { + console.error( + "It looks like you've accidentally thrown an error constructor function from a mock handler without calling it!" + ) + } + // rethrow so everything breaks because this isn't supposed to happen + throw thrown } } diff --git a/package-lock.json b/package-lock.json index e75f3ec38f..6cb4fd8bc4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -56,7 +56,7 @@ "@ianvs/prettier-plugin-sort-imports": "^4.3.0", "@ladle/react": "^4.1.0", "@mswjs/http-middleware": "^0.10.1", - "@oxide/openapi-gen-ts": "~0.2.2", + "@oxide/openapi-gen-ts": "~0.3.0", "@playwright/test": "^1.45.0", "@testing-library/dom": "^10.2.0", "@testing-library/jest-dom": "^6.4.6", @@ -1979,10 +1979,11 @@ } }, "node_modules/@oxide/openapi-gen-ts": { - "version": "0.2.2", - "resolved": "https://registry.npmjs.org/@oxide/openapi-gen-ts/-/openapi-gen-ts-0.2.2.tgz", - "integrity": "sha512-PHJS2Gk98DfeMN0kMLGlL8Hy3SNkF4RGnPxMs/q8FJsmAFHtJu7hqoPpFIhfpjFYdFOP7gEKbWilstheBYdTUA==", + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@oxide/openapi-gen-ts/-/openapi-gen-ts-0.3.0.tgz", + "integrity": "sha512-BiblEUJd+E3HgCmL3xZRtWXQ8AgaFVJ9KDm63nNt8pCg+HFpsTiEIJrlN1vaHx37gPkWk/Oi7tXGJOmPrpHfZg==", "dev": true, + "license": "MPL-2.0", "dependencies": { "minimist": "^1.2.8", "prettier": "2.7.1", @@ -20081,9 +20082,9 @@ } }, "@oxide/openapi-gen-ts": { - "version": "0.2.2", - "resolved": "https://registry.npmjs.org/@oxide/openapi-gen-ts/-/openapi-gen-ts-0.2.2.tgz", - "integrity": "sha512-PHJS2Gk98DfeMN0kMLGlL8Hy3SNkF4RGnPxMs/q8FJsmAFHtJu7hqoPpFIhfpjFYdFOP7gEKbWilstheBYdTUA==", + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@oxide/openapi-gen-ts/-/openapi-gen-ts-0.3.0.tgz", + "integrity": "sha512-BiblEUJd+E3HgCmL3xZRtWXQ8AgaFVJ9KDm63nNt8pCg+HFpsTiEIJrlN1vaHx37gPkWk/Oi7tXGJOmPrpHfZg==", "dev": true, "requires": { "minimist": "^1.2.8", diff --git a/package.json b/package.json index 9df3666a0c..15e662eecb 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,7 @@ "@ianvs/prettier-plugin-sort-imports": "^4.3.0", "@ladle/react": "^4.1.0", "@mswjs/http-middleware": "^0.10.1", - "@oxide/openapi-gen-ts": "~0.2.2", + "@oxide/openapi-gen-ts": "~0.3.0", "@playwright/test": "^1.45.0", "@testing-library/dom": "^10.2.0", "@testing-library/jest-dom": "^6.4.6",