Skip to content

Eliminate if (err instanceof Error) pattern through helper function #4207

@traeok

Description

@traeok

Is your feature request related to a problem? Please describe.

When you have to handle an error in a TypeScript try/catch block, you need to verify that the parameter in the catch block is an Error before accessing any properties on it. If the value thrown is not instanceof Error we can just throw it as-is. We have this pattern in numerous places in the codebase.

Describe the solution you'd like

Implement a helper function to reduce duplicating this pattern.

Describe alternatives you've considered

Can continue adopting this pattern:

try {
    // some action that can throw error
} catch (err) {
    if (err instanceof Error) {
        // transform or doing something special
    }
    throw err;
}

But this pattern can be easily de-duplicated

Additional context

Came up while reviewing code in zowe/zowex

Metadata

Metadata

Assignees

Labels

Technical DebtIncludes Architecture, Code, Testing, Automation debtenhancementNew feature or requestpriority-lowLegit issue but cosmetic or nice-to-have

Type

No type
No fields configured for issues without a type.

Projects

Status
Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions