Skip to content

Commit 71e0e39

Browse files
committed
refactor: clean up error handling and panic messages
- Replace verbose error messages with concise static strings for no_std. - Refactor panic and error handling for better compatibility and clarity. - Remove unnecessary formatting and string allocations in error paths. - Improve code consistency and maintainability across modules.
1 parent 2539db9 commit 71e0e39

36 files changed

+3765
-1986
lines changed

wrt-error/src/codes.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ pub const EXECUTION_INVALID_FRAME: u16 = 1014;
4242
/// Execution reader not implemented error
4343
pub const EXECUTION_READER_NOT_IMPLEMENTED: u16 = 1015;
4444
/// Capacity exceeded
45-
pub const CAPACITY_EXCEEDED: u16 = 1013;
45+
pub const CAPACITY_EXCEEDED: u16 = 1016;
4646
/// Gas limit exceeded
47-
pub const GAS_LIMIT_EXCEEDED: u16 = 1014;
47+
pub const GAS_LIMIT_EXCEEDED: u16 = 1017;
4848
/// Call stack exhausted
49-
pub const CALL_STACK_EXHAUSTED: u16 = 1015;
49+
pub const CALL_STACK_EXHAUSTED: u16 = 1018;
5050

5151
// Component model error codes (2000-2999)
5252
/// Invalid function index error
@@ -181,7 +181,7 @@ pub const TYPE_RESULT_COUNT_MISMATCH: u16 = 6019;
181181
pub const TYPE_RESULT_TYPE_MISMATCH: u16 = 6020;
182182
/// Invalid byte length for a given type or operation
183183
pub const INVALID_BYTE_LENGTH: u16 = 6021;
184-
/// Capacity of a bounded collection (e.g., BoundedVec, BoundedString) was
184+
/// Capacity of a bounded collection (e.g., `BoundedVec`, `BoundedString`) was
185185
/// exceeded during an operation like push or extend.
186186
pub const BOUNDED_COLLECTION_CAPACITY: u16 = 6022;
187187

@@ -292,6 +292,8 @@ pub const VALIDATION_MEMORY_ACCESS_ERROR: u16 = 8214;
292292
pub const VALIDATION_START_FUNCTION_ERROR: u16 = 8215;
293293

294294
// Memory errors (8400-8499)
295+
/// General memory error
296+
pub const MEMORY_ERROR: u16 = 8400;
295297
/// Memory allocation error
296298
pub const MEMORY_ALLOCATION_ERROR: u16 = 8403;
297299
/// Memory grow failure error
@@ -337,6 +339,10 @@ pub const SYSTEM_RESOURCE_LIMIT_ERROR: u16 = 8802;
337339
/// System unsupported feature error
338340
pub const SYSTEM_UNSUPPORTED_FEATURE_ERROR: u16 = 8803;
339341

342+
// Security errors (8900-8999)
343+
/// Control Flow Integrity violation
344+
pub const CFI_VIOLATION: u16 = 8900;
345+
340346
// Component errors (9000-9099)
341347
/// Component invalid type error
342348
pub const COMPONENT_INVALID_TYPE_ERROR: u16 = 9001;

wrt-error/src/errors.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub enum ErrorCategory {
4949
Capacity = 12,
5050
/// WebAssembly trap errors (specific runtime errors defined by Wasm spec)
5151
RuntimeTrap = 13,
52+
/// Initialization errors
53+
Initialization = 14,
5254
}
5355

5456
/// Base trait for all error types - `no_std` version
@@ -194,6 +196,48 @@ impl Error {
194196
Self::new(ErrorCategory::Type, codes::INVALID_TYPE, message)
195197
}
196198

199+
/// Create an index out of bounds error
200+
#[must_use]
201+
pub const fn index_out_of_bounds(message: &'static str) -> Self {
202+
Self::new(ErrorCategory::Memory, codes::OUT_OF_BOUNDS_ERROR, message)
203+
}
204+
205+
/// Create a deserialization error
206+
#[must_use]
207+
pub const fn deserialization_error(message: &'static str) -> Self {
208+
Self::new(ErrorCategory::Parse, codes::DECODING_ERROR, message)
209+
}
210+
211+
/// Create a capacity error
212+
#[must_use]
213+
pub const fn capacity_error(message: &'static str) -> Self {
214+
Self::new(ErrorCategory::Capacity, codes::CAPACITY_EXCEEDED, message)
215+
}
216+
217+
/// Create an internal error
218+
#[must_use]
219+
pub const fn internal_error(message: &'static str) -> Self {
220+
Self::new(ErrorCategory::System, codes::SYSTEM_ERROR, message)
221+
}
222+
223+
/// Create a memory out of bounds error
224+
#[must_use]
225+
pub const fn memory_out_of_bounds(message: &'static str) -> Self {
226+
Self::new(ErrorCategory::Memory, codes::MEMORY_OUT_OF_BOUNDS, message)
227+
}
228+
229+
/// Create a memory uninitialized error
230+
#[must_use]
231+
pub const fn memory_uninitialized(message: &'static str) -> Self {
232+
Self::new(ErrorCategory::Memory, codes::INITIALIZATION_ERROR, message)
233+
}
234+
235+
/// Create a new static error with explicit parameters
236+
#[must_use]
237+
pub const fn new_static(category: ErrorCategory, code: u16, message: &'static str) -> Self {
238+
Self::new(category, code, message)
239+
}
240+
197241
// Note: Methods like `with_message`, `new_legacy`, `*_with_code`,
198242
// and `parse_error_from_kind` have been removed as they were
199243
// dependent on `alloc` or dynamic messages not suitable for `&'static str`.

wrt-error/src/kinds.rs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -602,25 +602,41 @@ pub const fn tail_call_error(message: &'static str) -> TailCallError {
602602

603603
impl core::fmt::Display for ValidationError {
604604
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
605-
write!(f, "{}", self.0)
605+
if self.0.is_empty() {
606+
write!(f, "Validation error")
607+
} else {
608+
write!(f, "{}", self.0)
609+
}
606610
}
607611
}
608612

609613
impl core::fmt::Display for OutOfBoundsError {
610614
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
611-
write!(f, "{}", self.0)
615+
if self.0.is_empty() {
616+
write!(f, "Out of bounds error")
617+
} else {
618+
write!(f, "{}", self.0)
619+
}
612620
}
613621
}
614622

615623
impl core::fmt::Display for ParseError {
616624
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
617-
write!(f, "{}", self.0)
625+
if self.0.is_empty() {
626+
write!(f, "Parse error")
627+
} else {
628+
write!(f, "{}", self.0)
629+
}
618630
}
619631
}
620632

621633
impl core::fmt::Display for InvalidType {
622634
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
623-
write!(f, "{}", self.0)
635+
if self.0.is_empty() {
636+
write!(f, "Invalid type")
637+
} else {
638+
write!(f, "{}", self.0)
639+
}
624640
}
625641
}
626642

@@ -668,25 +684,41 @@ impl core::fmt::Display for InvalidLocalIndexError {
668684

669685
impl core::fmt::Display for ResourceError {
670686
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
671-
write!(f, "{}", self.0)
687+
if self.0.is_empty() {
688+
write!(f, "Resource error")
689+
} else {
690+
write!(f, "{}", self.0)
691+
}
672692
}
673693
}
674694

675695
impl core::fmt::Display for ComponentError {
676696
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
677-
write!(f, "{}", self.0)
697+
if self.0.is_empty() {
698+
write!(f, "Component error")
699+
} else {
700+
write!(f, "{}", self.0)
701+
}
678702
}
679703
}
680704

681705
impl core::fmt::Display for RuntimeError {
682706
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
683-
write!(f, "{}", self.0)
707+
if self.0.is_empty() {
708+
write!(f, "Runtime error")
709+
} else {
710+
write!(f, "{}", self.0)
711+
}
684712
}
685713
}
686714

687715
impl core::fmt::Display for PoisonedLockError {
688716
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
689-
write!(f, "{}", self.0)
717+
if self.0.is_empty() {
718+
write!(f, "Poisoned lock error")
719+
} else {
720+
write!(f, "{}", self.0)
721+
}
690722
}
691723
}
692724

@@ -716,7 +748,11 @@ impl core::fmt::Display for ArithmeticError {
716748

717749
impl core::fmt::Display for MemoryAccessError {
718750
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
719-
write!(f, "{}", self.0)
751+
if self.0.is_empty() {
752+
write!(f, "Memory access error")
753+
} else {
754+
write!(f, "{}", self.0)
755+
}
720756
}
721757
}
722758

0 commit comments

Comments
 (0)