Skip to content

Commit

Permalink
refactor!: the TxSubmit endpoint no longer adds the stack trace when …
Browse files Browse the repository at this point in the history
…returning domain errors

BREAKING CHANGE:

- stack property of returned errors was removed
  • Loading branch information
AngelCastilloB committed Mar 16, 2023
1 parent 48eba64 commit b145e1c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
22 changes: 18 additions & 4 deletions packages/ogmios/src/CardanoNode/OgmiosCardanoNode.ts
Expand Up @@ -17,7 +17,7 @@ import {
getServerHealth
} from '@cardano-ogmios/client';
import { Logger } from 'ts-log';
import { RunnableModule, contextLogger } from '@cardano-sdk/util';
import { RunnableModule, contextLogger, stripStackTrace } from '@cardano-sdk/util';
import { createInteractionContextWithLogger } from '../util';
import { queryEraSummaries } from './queries';

Expand Down Expand Up @@ -65,7 +65,12 @@ export class OgmiosCardanoNode extends RunnableModule implements CardanoNode {
this.#logger.info('Getting system start');
return await this.#stateQueryClient.systemStart();
} catch (error) {
throw CardanoNodeUtil.asCardanoNodeError(error) || new CardanoNodeErrors.UnknownCardanoNodeError(error);
const domainError =
CardanoNodeUtil.asCardanoNodeError(error) || new CardanoNodeErrors.UnknownCardanoNodeError(error);

stripStackTrace(domainError);

throw domainError;
}
}

Expand All @@ -85,7 +90,12 @@ export class OgmiosCardanoNode extends RunnableModule implements CardanoNode {
}
return map;
} catch (error) {
throw CardanoNodeUtil.asCardanoNodeError(error) || new CardanoNodeErrors.UnknownCardanoNodeError(error);
const domainError =
CardanoNodeUtil.asCardanoNodeError(error) || new CardanoNodeErrors.UnknownCardanoNodeError(error);

stripStackTrace(domainError);

throw domainError;
}
}

Expand All @@ -106,7 +116,11 @@ export class OgmiosCardanoNode extends RunnableModule implements CardanoNode {
if (error.name === 'FetchError') {
return { ok: false };
}
throw new ProviderError(ProviderFailure.Unknown, error);
const domainError = new ProviderError(ProviderFailure.Unknown, error);

stripStackTrace(domainError);

throw domainError;
}
}

Expand Down
Expand Up @@ -19,7 +19,7 @@ import {
getServerHealth
} from '@cardano-ogmios/client';
import { Logger } from 'ts-log';
import { RunnableModule, contextLogger, isNotNil } from '@cardano-sdk/util';
import { RunnableModule, contextLogger, isNotNil, stripStackTrace } from '@cardano-sdk/util';
import { createInteractionContextWithLogger } from '../../util';

/**
Expand Down Expand Up @@ -75,7 +75,12 @@ export class OgmiosTxSubmitProvider extends RunnableModule implements TxSubmitPr
);
await this.#txSubmissionClient.submitTx(signedTransaction);
} catch (error) {
throw Cardano.util.asTxSubmissionError(error) || new CardanoNodeErrors.UnknownTxSubmissionError(error);
const domainError =
Cardano.util.asTxSubmissionError(error) || new CardanoNodeErrors.UnknownTxSubmissionError(error);

stripStackTrace(domainError);

throw domainError;
}
}

Expand All @@ -96,7 +101,11 @@ export class OgmiosTxSubmitProvider extends RunnableModule implements TxSubmitPr
if (error.name === 'FetchError') {
return { ok: false };
}
throw new ProviderError(ProviderFailure.Unknown, error);
const domainError = new ProviderError(ProviderFailure.Unknown, error);

stripStackTrace(domainError);

throw domainError;
}
}

Expand Down
27 changes: 27 additions & 0 deletions packages/util/src/errors.ts
Expand Up @@ -5,6 +5,18 @@ interface ErrorLike {
stack: string;
}

interface HasInnerErrorLike {
innerError: string | Error;
}

/**
* Gets whether the given error has an innerError.
*
* @param error The error to be checked for.
*/
const isHasInnerErrorLike = (error: unknown): error is HasInnerErrorLike =>
error !== undefined && typeof error === 'object' && 'innerError' in (error as never);

/**
* This type check works as an "error instanceof Error" check, but it let pass also those objects
* which implements the Error interface without inheriting from the same base class
Expand All @@ -22,6 +34,21 @@ const isErrorLike = (error: unknown): error is ErrorLike => {
return typeof message === 'string' && typeof stack === 'string';
};

/**
* Strips the stack trace of all errors and their inner errors recursively.
*
* @param error The error to be stripped of its stack trace.
*/
export const stripStackTrace = (error: unknown) => {
if (isErrorLike(error)) {
delete (error as Error).stack;
}

if (isHasInnerErrorLike(error)) {
stripStackTrace(error.innerError);
}
};

export class ComposableError<InnerError = unknown> extends CustomError {
private static stackDelimiter = '\n at ';

Expand Down

0 comments on commit b145e1c

Please sign in to comment.