Skip to content

Commit

Permalink
[SDK-core] Refactor SFError to follow more conventional Error object …
Browse files Browse the repository at this point in the history
…structure (superfluid-finance#960)

* Refactor SFError to follow more conventional Error object structure

* Change tsconfig's 'lib' to a more modern value

* Fix tests

* Rename error 'code' back to 'type'

* Add fallback for Error.cause and assert it in tests

* Better Subgraph error message

* no more [object Object]]

* Add 'caused by' to error message

* set allowJs to true

Co-authored-by: 0xdavinchee <[email protected]>
  • Loading branch information
kasparkallas and 0xdavinchee authored Jul 19, 2022
1 parent 763ca69 commit 84853ed
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 117 deletions.
18 changes: 17 additions & 1 deletion packages/sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

## [0.5.0] - 2022-07-14

## Added
- Support for SetEvent and Subgraph v1.4.4

## Fixes
- Compile AJV validations to prevent unsafe-eval and did not allow SDK-Core usage inside Google Chrome extension due to unsafe-eval CSP

### Changed
- `SFError` refactor to be more conventional. It inherits `Error` and uses `cause` to wrap internal errors.
- Remove serialized internal error from the messages of `SFError` (it's now just included in the `.cause` property)

### Breaking
- `SFError.errorObject` renamed to `SFError.cause`

## [0.4.4] - 2022-06-30

## Added
Expand Down Expand Up @@ -152,7 +167,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- New `SuperToken` class with `SuperToken` CRUD functionality and an underlying `Token` class with basic `ERC20` functionality
- New `BatchCall` class for creating and executing batch calls with supported `Operation's`

[Unreleased]: https://github.com/superfluid-finance/protocol-monorepo/compare/sdk-core%40v0.4.4...HEAD
[Unreleased]: https://github.com/superfluid-finance/protocol-monorepo/compare/sdk-core%40v0.5.0...HEAD
[0.5.0]: https://github.com/superfluid-finance/protocol-monorepo/compare/sdk-core%40v0.4.4...sdk-core%40v0.5.0
[0.4.4]: https://github.com/superfluid-finance/protocol-monorepo/compare/sdk-core%40v0.4.3...sdk-core%40v0.4.4
[0.4.3]: https://github.com/superfluid-finance/protocol-monorepo/compare/sdk-core%40v0.4.2...sdk-core%40v0.4.3
[0.4.2]: https://github.com/superfluid-finance/protocol-monorepo/compare/sdk-core%40v0.4.1...sdk-core%40v0.4.2
Expand Down
14 changes: 6 additions & 8 deletions packages/sdk-core/src/BatchCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,15 @@ export default class BatchCall {
if (!operationType) {
throw new SFError({
type: "UNSUPPORTED_OPERATION",
customMessage:
"The operation at index " + index + " is unsupported.",
message: "The operation at index " + index + " is unsupported.",
});
}

/* istanbul ignore next */
if (!populatedTransaction.to || !populatedTransaction.data) {
throw new SFError({
type: "MISSING_TRANSACTION_PROPERTIES",
customMessage:
"The transaction is missing the to or data property.",
message: "The transaction is missing the to or data property.",
});
}

Expand Down Expand Up @@ -132,8 +130,8 @@ export default class BatchCall {
} catch (err) {
throw new SFError({
type: "BATCH_CALL_ERROR",
customMessage: "There was an error executing your batch call:",
errorObject: err,
message: "There was an error executing your batch call:",
cause: err,
});
}
};
Expand All @@ -159,8 +157,8 @@ export default class BatchCall {
} catch (err) {
throw new SFError({
type: "BATCH_CALL_ERROR",
customMessage: "There was an error executing your batch call:",
errorObject: err,
message: "There was an error executing your batch call:",
cause: err,
});
}
};
Expand Down
24 changes: 12 additions & 12 deletions packages/sdk-core/src/ConstantFlowAgreementV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export default class ConstantFlowAgreementV1 {
} catch (err) {
throw new SFError({
type: "CFAV1_READ",
customMessage: "There was an error getting the flow",
errorObject: err,
message: "There was an error getting the flow",
cause: err,
});
}
};
Expand All @@ -98,9 +98,9 @@ export default class ConstantFlowAgreementV1 {
} catch (err) {
throw new SFError({
type: "CFAV1_READ",
customMessage:
message:
"There was an error getting the account flow information",
errorObject: err,
cause: err,
});
}
};
Expand All @@ -124,8 +124,8 @@ export default class ConstantFlowAgreementV1 {
} catch (err) {
throw new SFError({
type: "CFAV1_READ",
customMessage: "There was an error getting net flow",
errorObject: err,
message: "There was an error getting net flow",
cause: err,
});
}
};
Expand Down Expand Up @@ -156,8 +156,8 @@ export default class ConstantFlowAgreementV1 {
} catch (err) {
throw new SFError({
type: "CFAV1_READ",
customMessage: "There was an error getting flow operator data",
errorObject: err,
message: "There was an error getting flow operator data",
cause: err,
});
}
};
Expand Down Expand Up @@ -187,8 +187,8 @@ export default class ConstantFlowAgreementV1 {
} catch (err) {
throw new SFError({
type: "CFAV1_READ",
customMessage: "There was an error getting flow operator data",
errorObject: err,
message: "There was an error getting flow operator data",
cause: err,
});
}
};
Expand Down Expand Up @@ -300,14 +300,14 @@ export default class ConstantFlowAgreementV1 {
if (!isPermissionsClean(params.permissions)) {
throw new SFError({
type: "UNCLEAN_PERMISSIONS",
customMessage: "The desired permissions are unclean",
message: "The desired permissions are unclean",
});
}

if (Number(params.flowRateAllowance) < 0) {
throw new SFError({
type: "NEGATIVE_FLOW_ALLOWANCE",
customMessage: "No negative flow allowance allowed",
message: "No negative flow allowance allowed",
});
}

Expand Down
20 changes: 10 additions & 10 deletions packages/sdk-core/src/ERC20Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export default class ERC20Token {
} catch (err) {
throw new SFError({
type: "SUPERTOKEN_READ",
customMessage: "There was an error getting allowance",
errorObject: err,
message: "There was an error getting allowance",
cause: err,
});
}
};
Expand All @@ -75,8 +75,8 @@ export default class ERC20Token {
} catch (err) {
throw new SFError({
type: "SUPERTOKEN_READ",
customMessage: "There was an error getting balanceOf",
errorObject: err,
message: "There was an error getting balanceOf",
cause: err,
});
}
};
Expand All @@ -97,8 +97,8 @@ export default class ERC20Token {
} catch (err) {
throw new SFError({
type: "SUPERTOKEN_READ",
customMessage: "There was an error getting name",
errorObject: err,
message: "There was an error getting name",
cause: err,
});
}
};
Expand All @@ -121,8 +121,8 @@ export default class ERC20Token {
} catch (err) {
throw new SFError({
type: "SUPERTOKEN_READ",
customMessage: "There was an error getting symbol",
errorObject: err,
message: "There was an error getting symbol",
cause: err,
});
}
};
Expand All @@ -145,8 +145,8 @@ export default class ERC20Token {
} catch (err) {
throw new SFError({
type: "SUPERTOKEN_READ",
customMessage: "There was an error getting totalSupply",
errorObject: err,
message: "There was an error getting totalSupply",
cause: err,
});
}
};
Expand Down
23 changes: 11 additions & 12 deletions packages/sdk-core/src/Framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export default class Framework {
if (network.chainId !== chainId && chainId != null) {
throw new SFError({
type: "NETWORK_MISMATCH",
customMessage:
message:
"Your provider network chainId is: " +
network.chainId +
" whereas your desired chainId is: " +
Expand Down Expand Up @@ -209,8 +209,8 @@ export default class Framework {
} catch (err) {
throw new SFError({
type: "FRAMEWORK_INITIALIZATION",
customMessage: "There was an error initializing the framework",
errorObject: err,
message: "There was an error initializing the framework",
cause: err,
});
}
};
Expand All @@ -232,8 +232,7 @@ export default class Framework {
) {
throw new SFError({
type: "CREATE_SIGNER",
customMessage:
"You must pass in a private key, provider or signer.",
message: "You must pass in a private key, provider or signer.",
});
}

Expand All @@ -242,7 +241,7 @@ export default class Framework {
if (!options.provider) {
throw new SFError({
type: "CREATE_SIGNER",
customMessage:
message:
"You must pass in a provider with your private key.",
});
}
Expand All @@ -258,7 +257,7 @@ export default class Framework {
/* istanbul ignore next */
throw new SFError({
type: "CREATE_SIGNER",
customMessage: "Something went wrong, this should never occur.",
message: "Something went wrong, this should never occur.",
});
};

Expand Down Expand Up @@ -290,7 +289,7 @@ export default class Framework {
if (!isNativeAssetSuperToken) {
throw new SFError({
type: "SUPERTOKEN_INITIALIZATION",
customMessage: "The token is not a native asset supertoken.",
message: "The token is not a native asset supertoken.",
});
}
return superToken as NativeAssetSuperToken;
Expand All @@ -312,7 +311,7 @@ export default class Framework {
if (!isPureSuperToken) {
throw new SFError({
type: "SUPERTOKEN_INITIALIZATION",
customMessage: "The token is not a pure supertoken.",
message: "The token is not a pure supertoken.",
});
}
return superToken as PureSuperToken;
Expand All @@ -333,7 +332,7 @@ export default class Framework {
if (!isWrapperSuperToken) {
throw new SFError({
type: "SUPERTOKEN_INITIALIZATION",
customMessage: "The token is not a wrapper supertoken.",
message: "The token is not a wrapper supertoken.",
});
}
return superToken as WrapperSuperToken;
Expand Down Expand Up @@ -384,11 +383,11 @@ export default class Framework {
} catch (err) {
throw new SFError({
type: "SUPERTOKEN_INITIALIZATION",
customMessage:
message:
"There was an error with loading the SuperToken with symbol: " +
tokenAddressOrSymbol +
" with the resolver.",
errorObject: err,
cause: err,
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk-core/src/InstantDistributionAgreementV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export default class InstantDistributionAgreementV1 {
} catch (err) {
throw new SFError({
type: "IDAV1_READ",
customMessage: "There was an error getting the subscription",
errorObject: err,
message: "There was an error getting the subscription",
cause: err,
});
}
};
Expand Down Expand Up @@ -111,8 +111,8 @@ export default class InstantDistributionAgreementV1 {
} catch (err) {
throw new SFError({
type: "IDAV1_READ",
customMessage: "There was an error getting the index",
errorObject: err,
message: "There was an error getting the index",
cause: err,
});
}
};
Expand Down
12 changes: 6 additions & 6 deletions packages/sdk-core/src/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export default class Operation {
} catch (err) {
throw new SFError({
type: "EXECUTE_TRANSACTION",
customMessage: "There was an error executing the transaction",
errorObject: err,
message: "There was an error executing the transaction",
cause: err,
});
}
};
Expand All @@ -65,8 +65,8 @@ export default class Operation {
/* istanbul ignore next */
throw new SFError({
type: "POPULATE_TRANSACTION",
customMessage: "There was an error populating the transaction",
errorObject: err,
message: "There was an error populating the transaction",
cause: err,
});
}
};
Expand All @@ -86,8 +86,8 @@ export default class Operation {
} catch (err) {
throw new SFError({
type: "SIGN_TRANSACTION",
customMessage: "There was an error signing the transaction",
errorObject: err,
message: "There was an error signing the transaction",
cause: err,
});
}
};
Expand Down
Loading

0 comments on commit 84853ed

Please sign in to comment.