1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
// Copyright 2020 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Errors specific to extension callback methods.

use std::fmt;

pub use crate::common::{Error, Result};

/// An error at the initialization stage of the WebAssembly module.
#[derive(Debug)]
pub(crate) enum ModuleError {
    /// WebAssembly module attempted to register 2 different extensions
    /// under the same `root_id`.
    DuplicateRegistration(String),
}

impl fmt::Display for ModuleError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use ModuleError::*;
        match self {
            DuplicateRegistration(name) => write!(
                f,
                "WebAssembly module attempted to register 2 different extensions under the same `root_id` \"{}\"",
                name,
            ),
        }
    }
}

impl std::error::Error for ModuleError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}

/// An error at the extension configuration stage.
#[derive(Debug)]
pub(crate) enum ConfigurationError {
    /// Envoy configuration uses a `root_id` value that is not present
    /// in this WebAssembly module.
    UnknownExtension {
        requested: String,
        available: Vec<String>,
    },
}

impl fmt::Display for ConfigurationError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use ConfigurationError::*;
        match self {
            UnknownExtension { requested, available } => write!(
                f,
                "WebAssembly module has no extension with `root_id` \"{}\"; valid `root_id` values are: {:?}",
                requested, available
            ),
        }
    }
}

impl std::error::Error for ConfigurationError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}

pub(crate) trait ErrorSink {
    fn observe(&self, context: &str, err: &Error);
}

impl dyn ErrorSink {
    /// Returns the default implementation that interacts with `Envoy`
    /// through its [`ABI`].
    ///
    /// [`ABI`]: https://github.com/proxy-wasm/spec
    pub fn default() -> &'static dyn ErrorSink {
        &impls::DefaultErrorSink
    }
}

mod impls {
    use super::{Error, ErrorSink};
    use crate::host::log;

    pub(super) struct DefaultErrorSink;

    impl ErrorSink for DefaultErrorSink {
        fn observe(&self, context: &str, err: &Error) {
            log::error!("{}: {}", context, err);
        }
    }
}