wapc_guest/errors.rs
1// Copyright 2015-2020 Capital One Services, LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! # Errors
16//!
17//! This module contains types and utility functions for error handling
18
19use std::error::Error as StdError;
20use std::fmt;
21
22/// This crate's Error type
23#[derive(Debug)]
24pub struct Error(ErrorKind);
25
26/// Create a new [Error] of the passed kind.
27#[must_use]
28pub fn new(kind: ErrorKind) -> Error {
29 Error(kind)
30}
31
32/// The kinds of errors this crate returns.
33#[derive(Debug)]
34pub enum ErrorKind {
35 /// Error returned when a host call fails.
36 HostError(Vec<u8>),
37}
38
39impl StdError for Error {}
40
41impl fmt::Display for Error {
42 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43 match self.0 {
44 ErrorKind::HostError(ref e) => write!(f, "Host error: {}", String::from_utf8_lossy(e)),
45 }
46 }
47}