- better python errors
This commit is contained in:
+8
-1
@@ -1,6 +1,7 @@
|
||||
use strum::IntoStaticStr;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
#[derive(Debug, Error, IntoStaticStr)]
|
||||
pub enum Error {
|
||||
#[error(transparent)]
|
||||
IO(#[from] std::io::Error),
|
||||
@@ -113,3 +114,9 @@ pub enum Error {
|
||||
#[error("cannot remove axes {0}, size {1} != 1")]
|
||||
SizeMismatch(String, usize),
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn variant_name(&self) -> &'static str {
|
||||
self.into()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,9 @@ use numpy::{
|
||||
use ome_metadata::Ome;
|
||||
use postcard::{from_bytes, to_stdvec};
|
||||
use pyo3::IntoPyObjectExt;
|
||||
use pyo3::exceptions::{PyIndexError, PyNotImplementedError, PyTypeError, PyValueError};
|
||||
use pyo3::exceptions::{
|
||||
PyIndexError, PyNotImplementedError, PyRuntimeError, PyTypeError, PyValueError,
|
||||
};
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::types::{
|
||||
PyBytes, PyEllipsis, PyInt, PyList, PyNone, PySlice, PySliceMethods, PyString, PyTuple,
|
||||
@@ -24,11 +26,25 @@ use pyo3_stub_gen::inventory::submit;
|
||||
use pyo3_stub_gen::{StubGenConfig, StubInfo};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashSet;
|
||||
use std::error::Error as StdError;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn format_error_chain(err: &crate::error::Error) -> String {
|
||||
let mut msg = format!("[{}] {err}", err.variant_name());
|
||||
let mut source = err.source();
|
||||
while let Some(s) = source {
|
||||
msg.push_str(&format!("\n |-> {s}"));
|
||||
source = s.source();
|
||||
}
|
||||
msg
|
||||
}
|
||||
|
||||
impl From<crate::error::Error> for PyErr {
|
||||
#[track_caller]
|
||||
fn from(err: crate::error::Error) -> PyErr {
|
||||
color_eyre::eyre::Report::from(err).into()
|
||||
let location = std::panic::Location::caller();
|
||||
let msg = format!("{}: {}", location, format_error_chain(&err));
|
||||
PyRuntimeError::new_err(msg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,7 +216,7 @@ impl PyView {
|
||||
let dtype_str = name.extract::<String>()?;
|
||||
dtype_str.parse()?
|
||||
} else {
|
||||
PixelType::U16
|
||||
*view.pixel_type()
|
||||
};
|
||||
let ome = view.metadata()?;
|
||||
Ok(Self {
|
||||
|
||||
Reference in New Issue
Block a user