Skip to content

feat: surface API 401 error message with login suggestion in portal generation#297

Open
MuhammadRafay1 wants to merge 3 commits into
devfrom
error-message-enhancement
Open

feat: surface API 401 error message with login suggestion in portal generation#297
MuhammadRafay1 wants to merge 3 commits into
devfrom
error-message-enhancement

Conversation

@MuhammadRafay1

@MuhammadRafay1 MuhammadRafay1 commented Jul 14, 2026

Copy link
Copy Markdown

Problem

Running portal commands without a valid session (e.g. apimatic portal serve while logged out) showed the generic fallback:

An unexpected error occurred, please try again later. If the problem persists, please reach out to our team at support@apimatic.io

even though the API returns a clear reason ({"message": "Authorization has been denied for this request."}). The SDK throws its typed UnauthorizedResponseError for 401s, but handleServiceError only recognizes axios errors, so the generic ServerError.

Changes

  • portal-service.ts: In generatePortal's catch, handle ApiError with status 401 — extract the API's message from the deserialized error result and return a ServiceError that pairs it with a suggested fix. All other errors continue to flow through handleServiceError unchanged (kept scoped on purpose; message handling differs per flow).

  • service-error.ts: New ServiceError.unauthorized(apiMessage) factory (alongside badRequest/forbidden/notFound) that appends "run apimatic auth login or provide a valid auth key using the --auth-key flag" to the API's message.

Result

■ Portal Generation failed.
■ Authorization has been denied for this request. Please
to log in, or provide a valid auth key using the --auth-key flag.

Testing

  • Reproduced end-to-end: portal serve with an empty config dir (logged out) against the production API — enhanced message shown.
  • Verified the axios 401 path is unaffected: auth status with an invalid stored key still prints "Invalid API key provided." (relies on the ServiceError.UnAuthorized singleton identity check, which is preserved).
  • pnpm test: 45 passing, lint clean.

MuhammadRafay1 and others added 2 commits July 14, 2026 16:46
…eneration

Previously, running portal commands while logged out showed the generic
"An unexpected error occurred" message because the SDK's typed 401 error
(UnauthorizedResponseError) was not recognized by the axios-only
handleServiceError. Handle ApiError with status 401 in generatePortal,
extract the API's message, and pair it with a suggestion to run
`apimatic auth login` or pass `--auth-key`.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Add the contribution workflow (branching, local debugging/packaging,
README updates, CLI and Platform SDK release process, NPM tagging) as
an AI skill file, with package-manager commands updated from npm to
pnpm, and index it in CLAUDE.md and .ai/instructions.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…rviceError

Move the 401 message extraction out of generatePortal's catch and into
handleServiceError, which now recognizes SDK ApiError statuses (401/404/500)
alongside axios errors. This surfaces the API's 401 message + login hint for
every SDK-based flow, not just portal generation, and removes the unreachable
401 branch inside the ProblemDetailsError block.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants