summary refs log tree commit diff
path: root/src/error.rs
blob: f3f1ca9d37b9a35472825841e3ca9f1cb46bb65a (plain)
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
#![forbid(unsafe_code)]

#[derive(Debug)]
pub struct Error {
  pub message: String,
}
pub type Result<T> = std::result::Result<T, Error>;

impl std::fmt::Display for Error {
  fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>)
      -> std::result::Result<(), std::fmt::Error>
  {
    fmt.write_str(&self.message)
  }
}

impl From<winit::error::EventLoopError> for Error {
  fn from(e: winit::error::EventLoopError) -> Self {
    match e {
      winit::error::EventLoopError::NotSupported(e) => Self::from(e),
      winit::error::EventLoopError::Os(e) => Self::from(e),
      winit::error::EventLoopError::RecreationAttempt => Error {
        message:
            "There may only ever be a single winit event loop.".to_string()
      },
      winit::error::EventLoopError::ExitFailure(code) => Error {
        message:
            format!("Clean unhappy exit with code {} via winit event loop",
                    code)
      }
    }
  }
}

impl From<winit::error::NotSupportedError> for Error {
  fn from(e: winit::error::NotSupportedError) -> Self {
    Error {
      message:
          format!("The winit backend does not support an operation: {}",
                  e.to_string())
    }
  }
}

impl From<winit::error::OsError> for Error {
  fn from(e: winit::error::OsError) -> Self {
    Error {
      message: format!("The OS told winit about an error: {}", e.to_string())
    }
  }
}

impl From<libloading::Error> for Error {
  fn from(e: libloading::Error) -> Self {
    Error {
      message: format!("The dynamic object loader reported an error: {}",
                       e.to_string())
    }
  }
}

impl From<Box<dyn vulkanalia::loader::LoaderError>> for Error {
  fn from(e: Box<dyn vulkanalia::loader::LoaderError>) -> Self {
    Error {
      message: format!("The Vulkan loader reported an error: {}",
                       e.to_string())
    }
  }
}

impl From<vulkanalia::vk::ErrorCode> for Error {
  fn from(e: vulkanalia::vk::ErrorCode) -> Self {
    Error {
      message: format!("Vulkan gave an error code: {}", e.to_string())
    }
  }
}

impl From<vulkanalia::bytecode::BytecodeError> for Error {
  fn from(e: vulkanalia::bytecode::BytecodeError) -> Self {
    Error {
      message: match e {
        vulkanalia::bytecode::BytecodeError::Alloc =>
            "Unable to allocate buffer for SPIR-V bytecode.".to_string(),
        vulkanalia::bytecode::BytecodeError::Length(length) =>
            format!("Compiled SPIR-V bytecode has length {}, \
                    which means it's corrupt.", length)
      }
    }
  }
}

pub fn ignore_errors(mut body: impl FnMut() -> Result<()>) -> () {
  if let Err(e) = body() {
    eprintln!("Error: {}", e);
  }
}