Skip to content

fix(shared): throw UnknownAttachmentDataError for unhandled AttachmentDescriptor payloads - #588

Closed
abhayrajjais01 wants to merge 5 commits into
hyperledger-identus:mainfrom
abhayrajjais01:fix/shared-unknown-attachment-error
Closed

fix(shared): throw UnknownAttachmentDataError for unhandled AttachmentDescriptor payloads#588
abhayrajjais01 wants to merge 5 commits into
hyperledger-identus:mainfrom
abhayrajjais01:fix/shared-unknown-attachment-error

Conversation

@abhayrajjais01

@abhayrajjais01 abhayrajjais01 commented May 3, 2026

Copy link
Copy Markdown

Description

AttachmentDescriptor.extractJSON threw a generic Error("Unhandled attachment") for payloads that were neither valid base64 JSON nor a recognizable json attachment shape. The domain layer already defines MercuryError.UnknownAttachmentDataError, but this path did not use it, so callers could not reliably branch on Mercury’s typed errors.

This PR throws UnknownAttachmentDataError from that fallback path with a short message describing the expected shapes, and adds a Vitest regression test (header-only attachment data) that expects MercuryError.UnknownAttachmentDataError.

Files: packages/shared/domain/src/models/MessageAttachment.ts, packages/shared/domain/tests/setup.ts, packages/shared/domain/tests/MessageAttachment.test.ts.

Alternatives Considered (optional)

  • Keep Error: Preserves old instanceof Error behavior but loses the domain error type and is inconsistent with existing Mercury error classes.
  • New error class: Unnecessary; UnknownAttachmentDataError already matches the semantic.

Checklist

  • My PR follows the contribution guidelines of this project
  • My PR is free of third-party dependencies that don't comply with the Allowlist
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked the PR title to follow the conventional commit specification

@abhayrajjais01
abhayrajjais01 requested a review from a team as a code owner May 3, 2026 11:37
@abhayrajjais01
abhayrajjais01 force-pushed the fix/shared-unknown-attachment-error branch from 502e858 to 20e609a Compare May 3, 2026 11:40
…tDescriptor payloads

Signed-off-by: abhayrajjais01 <abhayrajjais01@users.noreply.github.com>
@abhayrajjais01
abhayrajjais01 force-pushed the fix/shared-unknown-attachment-error branch from 20e609a to 7c50eab Compare May 3, 2026 11:41

@elribonazo elribonazo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing that you added should have any effect on the setup.ts files, why did you add that?

Comment thread packages/shared/domain/tests/setup.ts Outdated
@@ -0,0 +1,8 @@

import { Buffer } from 'buffer';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that needed really? Nothing that u added has any effect on this, am I wrong?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added tests/setup.ts only because vitest.config.js already references ./tests/setup.ts but that file was missing on main -> I can keep an empty/minimal setup file for that path, without the polyfill,
Buffer was already used in MessageAttachment, and Vitest’s jsdom env still runs under Node where Buffer is normally global, so I wil remove the buffer import and global assignment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elribonazo ,
what should i do??

1st - remove the Buffer and if-else block and write a tiny placeholder file so vitest.config.js still has a real ./tests/setup.ts

or
delete setup.ts and remove the setupFiles: [tests/setup.ts] line from vitest.config.js so Vitest doesn’t point at a missing file??

@yshyn-iohk

Copy link
Copy Markdown
Contributor

Thanks for your contribution, @abhayrajjais01!

The change itself is correct and clean. MessageAttachment.ts line 98–99 had:

// TODO better error
throw new Error("Unhandled attachment");

The domain layer already defines MercuryError.UnknownAttachmentDataError (in packages/shared/domain/src/models/errors/Mercury.ts:37) for exactly this semantic. The PR simply uses it — this is the right call. Since UnknownAttachmentDataError extends Error, any existing catch (e) blocks that check instanceof Error remain unaffected, so it's backward compatible.

The test is clean — it constructs a header-only AttachmentDescriptor (which is neither base64 nor JSON), calls extractJSON, and expects the typed error. Good regression coverage.

The setup.ts question

Javi's pushback is valid — the content of setup.ts (Buffer polyfill) is unnecessary. But the file does need to exist, and here's why:

packages/shared/domain/vitest.config.js already has:

setupFiles: ['./tests/setup.ts'],
passWithNoTests: true,

On main today, there are no test files in packages/shared/domain/tests/ — the directory doesn't even exist. Since passWithNoTests: true means vitest skips execution entirely when there are no matching test files, the missing setup.ts has never been a problem. But this PR adds the first test (MessageAttachment.test.ts), so vitest will now actually run — and it will fail when trying to load the missing setup.ts.

The right path forward: keep setup.ts as an empty file (or a single comment like // vitest setup), remove the Buffer polyfill. This satisfies the vitest config reference without adding unnecessary code.

// packages/shared/domain/tests/setup.ts
// intentionally empty – required by vitest.config.js setupFiles

@coveralls

coveralls commented May 5, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 75.224% (+0.03%) from 75.19% — abhayrajjais01:fix/shared-unknown-attachment-error into hyperledger-identus:main

Remove Buffer setup code for testing environment.

Signed-off-by: Abhayraj Jaiswal <abhayraj916146@gmail.com>
@abhayrajjais01

Copy link
Copy Markdown
Author

@yshyn-iohk,
Applied your suggestion
Thank you

@sonarqubecloud

sonarqubecloud Bot commented May 5, 2026

Copy link
Copy Markdown

@elribonazo

Copy link
Copy Markdown
Contributor

@abhayrajjais01 please check your commit signatures

@@ -0,0 +1,2 @@
// packages/shared/domain/tests/setup.ts

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove

@elribonazo

Copy link
Copy Markdown
Contributor

Closing due to inactivity

@elribonazo elribonazo closed this May 19, 2026
@abhayrajjais01

Copy link
Copy Markdown
Author

hi @elribonazo,
created a new pr addressing your suggestions, please take a look #642

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.

5 participants