Skip to content

[FEATURE] Clean up the error checks in the config context commands #2311

Description

@coderxaditya

Why do we need this improvement?

While going through the config context commands, I noticed that all six of them (add, list, edit, remove, use, current) catch errors like this:

if (e instanceof (MissingContextFileError || ContextFileWrongFormatError)) {

This only checks MissingContextFileError. The part in brackets runs first, and since a class is always truthy, it just returns MissingContextFileError. So ContextFileWrongFormatError is never actually checked here.

Right now the behaviour is correct, but only by accident. When the context file is broken, the error skips this branch and the user sees the real message ("Context file ... has wrong format"). The existing tests for add expect exactly that message.

The problem is that the code says something different from what it does. If someone tries to "fix" it to check both errors, users with a broken context file would be told they have no context file and should run asyncapi config context init. But init writes a new file without checking if one already exists, so they would lose their saved contexts. That change also makes 4 of the existing add tests fail, so it's an easy mistake to make.

There is also a small dead check in current.ts. (fileContent && !fileContent.current) is inside the catch block, but fileContent is always undefined there, so it can never be true.

How will this change help?

The code will say what it actually does, so it's clear that a broken context file should show the "wrong format" error and not the "run init" message. It also stops someone from bringing in the bug above during a future cleanup.

Right now only add has tests for a broken context file. Adding the same tests for the other five commands means this behaviour is covered everywhere.

Screenshots

No response

How could it be implemented/designed?

It's a small change:

  • In all six commands, replace e instanceof (MissingContextFileError || ContextFileWrongFormatError) with e instanceof MissingContextFileError
  • Remove the ContextFileWrongFormatError imports that become unused
  • Remove the dead (fileContent && !fileContent.current) check in current.ts
  • Add tests in test/integration/context.test.ts that run current, list, use, edit and remove with a broken context file and check that the "has wrong format" message is shown

Nothing changes for users. Happy to open a PR for this if it sounds good.

🚧 Breaking changes

No

👀 Have you checked for similar open issues?

  • I checked and didn't find a similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue?

Yes I am willing to submit a PR!

Activity

github-actions commented on Oct 4, 2026

@github-actions
Contributor

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

viky-01 commented on Oct 6, 2026

@viky-01

Hi @coderxaditya @asyncapi/cli-maintainers! I've opened PR #2312 addressing this cleanup across all 6 context commands along with the corresponding integration test coverage. All CI checks pass and it's ready for review.

coderxaditya commented on Oct 6, 2026

@coderxaditya
Author

Thanks for the interest @viky-01! Just a small note for the maintainers: I opened this issue and mentioned I'd like to work on it, and I was waiting for it to be triaged before opening a PR, as the contributing guide asks. I already have the change and tests ready locally (same approach as described in the issue). Happy to go with whatever you prefer.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions