From c249cd9745ad6cb8f7cea31eae8f9c8771947ac6 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Thu, 10 Aug 2023 17:22:24 -0300 Subject: [PATCH] Add SyscallError (#1357) * Add constant string to VirtualMachineError::Hint error * Add SycallError * update CHANGELOG.md * Update doc * Fix str constant --- CHANGELOG.md | 2 ++ docs/hint_processor/builtin_hint_processor/README.md | 2 +- vm/src/vm/errors/hint_errors.rs | 4 +++- vm/src/vm/errors/vm_errors.rs | 4 +++- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b34801fd6..86d6fb4064 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* Add HintError::SyscallError and VmErrors::HINT_ERROR_STR constant [#1357](https://github.com/lambdaclass/cairo-rs/pull/1357) + * feat: make *arbitrary* feature also enable a `proptest::arbitrary::Arbitrary` implementation for `Felt252` [#1355](https://github.com/lambdaclass/cairo-vm/pull/1355) * fix: correctly display invalid signature error message [#1361](https://github.com/lambdaclass/cairo-vm/pull/1361) diff --git a/docs/hint_processor/builtin_hint_processor/README.md b/docs/hint_processor/builtin_hint_processor/README.md index bded207697..e7212da729 100644 --- a/docs/hint_processor/builtin_hint_processor/README.md +++ b/docs/hint_processor/builtin_hint_processor/README.md @@ -122,7 +122,7 @@ There are also some helpers that dont depend on the hint processor used that can You can also find plenty of example implementations in the [builtin hint processor folder](../../../src/hint_processor/builtin_hint_processor). ### Error Handling -This API uses VirtualMachineError as error return type for hint functions, while its not possible to add error types to VirtualMachineError, you can use VirtualMachineError::CustomHint which receives a string and prints an error message with the format: "Custom Hint Error: [your message]". +This API uses VirtualMachineError as error return type for hint functions, while its not possible to add error types to VirtualMachineError, you can use VirtualMachineError::CustomHint which receives a string and prints an error message with the format: "Hint Error: [your message]". For example, if we want our hint to return an error if ids.a is less than 0 we could write: ```rust diff --git a/vm/src/vm/errors/hint_errors.rs b/vm/src/vm/errors/hint_errors.rs index d94c7680a2..bcf8935c5d 100644 --- a/vm/src/vm/errors/hint_errors.rs +++ b/vm/src/vm/errors/hint_errors.rs @@ -42,7 +42,7 @@ pub enum HintError { UnknownIdentifierInternal, #[error("Wrong identifier type at address {0}")] WrongIdentifierTypeInternal(Box), - #[error("Custom Hint Error: {0}")] + #[error("Hint Error: {0}")] CustomHint(Box), #[error("Missing constant: {0}")] MissingConstant(Box<&'static str>), @@ -182,6 +182,8 @@ pub enum HintError { InvalidValue(Box<(&'static str, Felt252, Felt252)>), #[error("Attempt to subtract with overflow: ids.m - 1")] NPairBitsTooLowM, + #[error("{0}")] + SyscallError(Box), } #[cfg(test)] diff --git a/vm/src/vm/errors/vm_errors.rs b/vm/src/vm/errors/vm_errors.rs index 267c6545f6..711180d88a 100644 --- a/vm/src/vm/errors/vm_errors.rs +++ b/vm/src/vm/errors/vm_errors.rs @@ -17,6 +17,8 @@ use crate::{ }; use felt::Felt252; +pub const HINT_ERROR_STR: &str = "Got an exception while executing a hint: "; + #[derive(Debug, Error)] pub enum VirtualMachineError { #[error(transparent)] @@ -111,7 +113,7 @@ pub enum VirtualMachineError { InvalidArgCount(Box<(usize, usize)>), #[error("Couldn't parse prime: {0}")] CouldntParsePrime(Box), - #[error("Got an exception while executing a hint: {}", (*.0).1)] + #[error("{HINT_ERROR_STR}{}", (*.0).1)] Hint(Box<(usize, HintError)>), #[error("Unexpected Failure")] Unexpected,