| Copyright | (c) 2026 Tushar Adhatrao |
|---|---|
| License | MIT |
| Stability | stable |
| Safe Haskell | None |
| Language | GHC2021 |
WasmEdge.Error
Description
This module defines WasmEdgeError, the exception type thrown by all
operations that fail in the WasmEdge C runtime. It also provides the
ErrCategory and ErrCode types that mirror the C API enums.
Most users only need to catch WasmEdgeError in an exception handler:
result <- try (runWasmFile vm "module.wasm" "main" []) :: IO (Either WasmEdgeError [WasmVal])
Synopsis
- data WasmEdgeError = WasmEdgeError {
- weErrCategory :: !ErrCategory
- weErrCode :: !Word32
- weErrMessage :: !Text
- type WasmResult a = Either WasmEdgeError a
- data ErrCategory
- mkErrCategory :: Word32 -> ErrCategory
- isSuccess :: WasmEdgeError -> Bool
- isUserError :: WasmEdgeError -> Bool
- formatError :: WasmEdgeError -> String
- throwOnLeft :: WasmResult a -> IO a
Error type
data WasmEdgeError #
An error returned by the WasmEdge runtime.
Thrown as a Haskell exception by all public API functions that encounter
a failed WasmEdge_Result. Can also be caught as SomeException.
Constructors
| WasmEdgeError | |
Fields
| |
Instances
type WasmResult a = Either WasmEdgeError a #
A computation that may return a WasmEdgeError.
Use throwOnLeft to re-raise as an exception, or pattern-match directly.
Error category
data ErrCategory #
Maps to the C WasmEdge_ErrCategory enum.
Constructors
| ErrCategory_WASM | Error from the WebAssembly spec (code is |
| ErrCategory_UserDefined | User-defined error code (e.g., from host functions) |
| ErrCategory_Unknown Word32 | An unrecognised category (future-proofing) |
Instances
| NFData ErrCategory # | |||||
Defined in WasmEdge.Error Methods rnf :: ErrCategory -> () # | |||||
| Generic ErrCategory # | |||||
Defined in WasmEdge.Error Associated Types
| |||||
| Show ErrCategory # | |||||
Defined in WasmEdge.Error Methods showsPrec :: Int -> ErrCategory -> ShowS # show :: ErrCategory -> String # showList :: [ErrCategory] -> ShowS # | |||||
| Eq ErrCategory # | |||||
Defined in WasmEdge.Error | |||||
| type Rep ErrCategory # | |||||
Defined in WasmEdge.Error type Rep ErrCategory = D1 ('MetaData "ErrCategory" "WasmEdge.Error" "wasmedge-hs-0.17.1.0-inplace" 'False) (C1 ('MetaCons "ErrCategory_WASM" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "ErrCategory_UserDefined" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "ErrCategory_Unknown" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Word32)))) | |||||
mkErrCategory :: Word32 -> ErrCategory #
Convert a raw C enum value (Word32) to ErrCategory.
Common predicates
isSuccess :: WasmEdgeError -> Bool #
True if the error has error code 0 (should not happen in practice, but guards against malformed results).
isUserError :: WasmEdgeError -> Bool #
True if this is a user-defined error from a host function.
formatError :: WasmEdgeError -> String #
Format a WasmEdgeError into a human-readable string with a hex error code.
Convenience
throwOnLeft :: WasmResult a -> IO a #
Throw a WasmEdgeError as an IO exception on Left, or unwrap Right.
Useful for step-by-step WASM pipelines where you want errors to propagate as exceptions rather than handling them inline:
withVM Nothing Nothing $ vm -> do loadWasmFile vm "module.wasm" >>= throwOnLeft validate vm >>= throwOnLeft instantiate vm >>= throwOnLeft execute vm "run" [] >>= throwOnLeft