Skip to content

Conversation

@sirtimid
Copy link
Contributor

@sirtimid sirtimid commented Oct 14, 2024

closes #150

Following #149, this PR modifies the marshaling functions in @ocap/streams such that errors from @ocap/errors are unmarshaled into their respective classes, based on the error code. It also extends the stringify utility to support ocap errors.

@sirtimid sirtimid force-pushed the sirtimid/un-marshal-custom-errors branch 2 times, most recently from 07f97ca to 3c7db2d Compare October 14, 2024 17:49
@socket-security
Copy link

socket-security bot commented Oct 15, 2024

@sirtimid sirtimid force-pushed the sirtimid/un-marshal-custom-errors branch from 7f0cadf to b622374 Compare October 15, 2024 19:22
Base automatically changed from sirtimid/custom-error-classes to main October 15, 2024 22:53
@sirtimid sirtimid force-pushed the sirtimid/un-marshal-custom-errors branch from b622374 to 77d6ca6 Compare October 16, 2024 14:29
@sirtimid sirtimid marked this pull request as ready for review October 16, 2024 14:29
@sirtimid sirtimid requested a review from a team as a code owner October 16, 2024 14:29
@sirtimid sirtimid requested a review from rekmarks October 16, 2024 14:29
Copy link
Member

@rekmarks rekmarks left a comment

Choose a reason for hiding this comment

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

Nice! I have various questions and suggestions. For all of the errors, I left a number of suggestions on VatNotFoundError that are generally applicable.

Two big changes are harden:ing the error prototypes, error instances, and marshaled errors. You may want to do that after any changes I propose about e.g. reorganizing the types / utils.

@sirtimid sirtimid force-pushed the sirtimid/un-marshal-custom-errors branch from e8d9eeb to b587495 Compare October 16, 2024 20:05
@sirtimid sirtimid requested a review from rekmarks October 17, 2024 10:51
@sirtimid sirtimid force-pushed the sirtimid/un-marshal-custom-errors branch from ff208cf to 93c4084 Compare October 17, 2024 14:23
@sirtimid sirtimid force-pushed the sirtimid/un-marshal-custom-errors branch from 93c4084 to 4a455f3 Compare October 17, 2024 15:40
Copy link
Member

@rekmarks rekmarks left a comment

Choose a reason for hiding this comment

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

Just a couple more things.

Comment on lines 31 to 53
/**
* Struct to validate marshaled errors.
*/
export const MarshaledErrorStruct = object({
[ErrorSentinel]: literal(true),
message: string(),
code: optional(ErrorCodeStruct),
data: optional(JsonStruct),
stack: optional(string()),
cause: optional(union([string(), lazy(() => MarshaledErrorStruct)])),
}) as Struct<MarshaledError>;

/**
* Base schema for validating Ocap error classes during error marshaling.
*/
export const baseErrorStructSchema = {
[ErrorSentinel]: literal(true),
message: string(),
code: ErrorCodeStruct,
data: JsonStruct,
stack: optional(string()),
cause: optional(union([string(), lazy(() => MarshaledErrorStruct)])),
};
Copy link
Member

Choose a reason for hiding this comment

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

See comment in isMarshaledOcapError.ts.

Suggested change
/**
* Struct to validate marshaled errors.
*/
export const MarshaledErrorStruct = object({
[ErrorSentinel]: literal(true),
message: string(),
code: optional(ErrorCodeStruct),
data: optional(JsonStruct),
stack: optional(string()),
cause: optional(union([string(), lazy(() => MarshaledErrorStruct)])),
}) as Struct<MarshaledError>;
/**
* Base schema for validating Ocap error classes during error marshaling.
*/
export const baseErrorStructSchema = {
[ErrorSentinel]: literal(true),
message: string(),
code: ErrorCodeStruct,
data: JsonStruct,
stack: optional(string()),
cause: optional(union([string(), lazy(() => MarshaledErrorStruct)])),
};
const marshaledErrorSchema = {
[ErrorSentinel]: literal(true),
message: string(),
data: optional(JsonStruct),
stack: optional(string()),
};
/**
* Struct to validate marshaled errors.
*/
export const MarshaledErrorStruct = object({
...marshaledErrorSchema,
cause: optional(union([string(), lazy(() => MarshaledErrorStruct)])),
}) as Struct<MarshaledError>;
/**
* Struct to validate marshaled ocap errors.
*/
export const MarshaledOcapErrorStruct = object({
...marshaledErrorSchema,
code: ErrorCodeStruct,
data: JsonStruct,
cause: optional(union([string(), lazy(() => MarshaledOcapErrorStruct)])),
}) as Struct<MarshaledOcapError>;
/**
* Base schema for validating Ocap error classes during error marshaling.
*/
export const baseErrorStructSchema = {
[ErrorSentinel]: literal(true),
message: string(),
code: ErrorCodeStruct,
data: JsonStruct,
stack: optional(string()),
cause: optional(union([string(), lazy(() => MarshaledErrorStruct)])),
};

Copy link
Member

@rekmarks rekmarks left a comment

Choose a reason for hiding this comment

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

LGTM!

@sirtimid sirtimid merged commit 474d285 into main Oct 17, 2024
@sirtimid sirtimid deleted the sirtimid/un-marshal-custom-errors branch October 17, 2024 19:32
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.

Marshal and unmarshal @ocap/errors

3 participants