Skip to content

Add support for unknown type errors. - #40

Open
ralucarusu-tc wants to merge 4 commits into
support/3.0from
fix/error-type-unknown
Open

Add support for unknown type errors.#40
ralucarusu-tc wants to merge 4 commits into
support/3.0from
fix/error-type-unknown

Conversation

@ralucarusu-tc

Copy link
Copy Markdown

This fix was required in order to add support for unknown error types, which are the standards within the try-catch syntaxes.

Co-authored-by: Copilot <copilot@github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the error utilities to better support unknown error values (as commonly produced/encouraged by modern try/catch patterns) and adjusts typings/tests accordingly.

Changes:

  • Broadened error/cause typing (unknown) and updated exported error-related interfaces.
  • Updated error parsing and network error handling to work with non-Error causes.
  • Updated TypeScript/Jest configuration and a few tests to satisfy stricter typings.

Reviewed changes

Copilot reviewed 7 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tsconfig.json Adds global Jest/Node types for compilation.
lib/errors/parse-error-to-readable-json.ts Changes parser input to unknown.
lib/errors/integration-error.ts Broadens cause and makes several fields optional; renames exported error interface.
lib/errors/network-error.ts Detects error codes from non-Error causes.
test/errors/network-error.test.ts Uses optional chaining with help to satisfy updated typing.
test/errors/integration-error.test.ts Adjusts deletion of cause.stack for unknown cause typing.
package.json / package-lock.json Bumps package version to 3.1.9.
etc/polarity-integration-utils.api.md Updates generated API report for the new public types.
Comments suppressed due to low confidence (1)

lib/errors/integration-error.ts:13

  • This change removes the previously exported Error type (replaced by ResponseError). Since lib/errors/index.ts re-exports this module, this is a breaking public API change for any consumers importing { Error }. Consider keeping a backwards-compatible alias export (e.g., export type Error = ResponseError) and deprecating it, or bumping the major version if you intend to remove it.
export interface ResponseError {
  name: string;
  message: string;
  stack?: string;
  code?: number | string;
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/errors/parse-error-to-readable-json.ts Outdated
Comment on lines 6 to 7
export const parseErrorToReadableJson = (error: unknown) =>
JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error)));

Copilot AI Apr 23, 2026

Copy link

Choose a reason for hiding this comment

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

parseErrorToReadableJson now accepts unknown, but Object.getOwnPropertyNames(error) will throw for null/undefined (both are valid values to be thrown/caught in JS). Add a nullish/type guard (or a safe fallback) so this helper doesn’t crash when called with non-object causes.

Suggested change
export const parseErrorToReadableJson = (error: unknown) =>
JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error)));
export const parseErrorToReadableJson = (error: unknown) => {
const replacer =
(typeof error === 'object' && error !== null) || typeof error === 'function'
? Object.getOwnPropertyNames(error)
: undefined;
return JSON.parse(JSON.stringify(error, replacer));
};

Copilot uses AI. Check for mistakes.
Comment thread lib/errors/integration-error.ts Outdated
Comment on lines 211 to 213
if (properties.cause) {
this.cause = properties.cause;
}

Copilot AI Apr 23, 2026

Copy link

Choose a reason for hiding this comment

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

cause is now unknown, but the constructor only assigns it when it is truthy. That drops valid falsey causes (e.g., 0, '', false, or even null if you decide to allow it) and makes behavior depend on the runtime value rather than whether the property was provided. Prefer checking for presence (e.g., typeof properties.cause !== 'undefined' or 'cause' in properties) so all provided values are preserved.

Copilot uses AI. Check for mistakes.
Comment thread package.json Outdated
{
"name": "polarity-integration-utils",
"version": "3.1.8",
"version": "3.1.9",

Copilot AI Apr 23, 2026

Copy link

Choose a reason for hiding this comment

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

The exported error types/properties were broadened (e.g., cause: unknown, multiple IntegrationError fields becoming optional, and the exported Error type removed). For TypeScript consumers this is a breaking API change, so a patch bump (3.1.8 -> 3.1.9) is likely insufficient under semver. Consider a minor/major bump depending on your compatibility policy (or add compatibility aliases to avoid breaking changes).

Suggested change
"version": "3.1.9",
"version": "3.2.0",

Copilot uses AI. Check for mistakes.
props.help = this.help;
}

if (this.cause) {

@sarus sarus Apr 23, 2026

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.

Should only call this is this.cause is of type Error otherwise we'll have an issue with the Object.getOwnPropertyNames() in parseErrorToReadableJson()

You can then keep the type for parseErrorToReadableJson stricter at just Error instead of also taking unknown.

Comment thread lib/errors/network-error.ts Outdated
if (properties.cause instanceof Error) {
const originalError = properties.cause;
const code = originalError.code?.toString();
if (properties.cause && typeof properties.cause === 'object' && 'code' in properties.cause) {

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.

I think the instanceof check here is better since the cause here should always be from wrapping an HTTP error thrown by the postman-request library. I can't think of a situation where someone would be passing in an object that has the same shape as a NodeJS HTTP Error but not using an actual NodeJS HTTP Error.

@sarus sarus 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.

I think some of the copilot feedback makes sense. Otherwise left two comments.

ralucarusu-tc and others added 3 commits April 23, 2026 12:22
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <copilot@github.com>
@sarus

sarus commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

@ralucarusu-tc Is this PR still relevant? Happy to re-review if so.

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.

3 participants