use crate::compiler::CompilationError;
use crate::server::{IoError, LaunchError, ServerError};
use alloc::string::ToString;
use cubecl_environment::backtrace::BackTrace;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DriverError {
op: &'static str,
status: u32,
}
impl DriverError {
pub fn new(op: &'static str, status: u32) -> Self {
Self { op, status }
}
pub fn op(&self) -> &'static str {
self.op
}
pub fn status(&self) -> u32 {
self.status
}
}
impl core::fmt::Display for DriverError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{} failed with status {}", self.op, self.status)
}
}
impl core::error::Error for DriverError {}
impl From<DriverError> for ServerError {
fn from(error: DriverError) -> Self {
ServerError::Generic {
reason: error.to_string(),
backtrace: BackTrace::capture(),
}
}
}
impl From<DriverError> for IoError {
fn from(error: DriverError) -> Self {
IoError::Unknown {
description: error.to_string(),
backtrace: BackTrace::capture(),
}
}
}
impl From<DriverError> for CompilationError {
fn from(error: DriverError) -> Self {
CompilationError::Generic {
reason: error.to_string(),
backtrace: BackTrace::capture(),
}
}
}
impl From<DriverError> for LaunchError {
fn from(error: DriverError) -> Self {
LaunchError::Unknown {
reason: error.to_string(),
backtrace: BackTrace::capture(),
}
}
}
pub fn checked(op: &'static str, status: u32) -> Result<(), DriverError> {
match status {
0 => Ok(()),
status => Err(DriverError::new(op, status)),
}
}