oxidecomputer / oxidecomputer/console
Mock API has no response body on `throw 403`
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 228
- Forks
- 22
- Avg merge
- 19h 42m
- Merged PRs (30d)
- 32
Description
We have logic in the wrapper for the mock server that detects when a number is thrown and treats it as a status code.
We use this in 5 spots:
$ rg 'throw \d' libs/api-mocks/msw
libs/api-mocks/msw/util.ts
344: * throw 403 if no.
352: * for the user as well as for the user's groups. Do nothing if yes, throw 403
363: if (!userHasRole(user, resourceType, resourceId, role)) throw 403
libs/api-mocks/msw/handlers.ts
108: if (body.name === 'disk-create-500') throw 500
160: if (disk.name === 'import-start-500') throw 500
166: // throw 400
175: if (disk.name === 'import-stop-500') throw 500
189: // if (Math.random() < 0.01) throw 400
196: if (disk.name === 'disk-finalize-500') throw 500
It's convenient, but the resulting responses have no body, and in our client-side error handling code, we are looking at error_code in the body rather than the status code to figure out what kind of error it is.
This means that in local dev and tests we get Unknown server error when we should be getting a message more appropriate for a 403.
The simple fix is to just change these spots to use a canned response object like these:
More involved would be to either have the mock API insert a body for certain error codes, or blow up when you try to throw a number so you can't use this shortcut anymore. The former sounds convenient but it would be pretty hard for someone to tell what happens when you throw a 403 if they didn't already know. I think it would be more readable to explicitly throw a canned 403 response.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the thrown status-code cases in libs/api-mocks/msw/util.ts and handlers.ts, then compare them with the canned response object in util.ts. Check libs/api/errors.ts to confirm which error_code values the client reads. Done means mocked 403 and 500 responses include bodies so client-side errors no longer report Unknown server error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100