feat(errors): add stable error propagation
This commit is contained in:
1 parent
97acb2a2d7
commit
ba9c97ed3b
34 files changed
+1007
No files matched your search
@@ -0,0 +1,8 @@
|
||||
use kaptan_err_propagate_macros::propagate;
|
||||
|
||||
#[propagate(report)]
|
||||
fn invalid() -> Result<(), ()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,7 @@
|
||||
error: kaptan_err_propagate::propagate does not accept arguments
|
||||
--> tests/ui/arguments.rs:3:1
|
||||
|
|
||||
3 | #[propagate(report)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: this error originates in the attribute macro `propagate` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
@@ -0,0 +1,8 @@
|
||||
use kaptan_err_propagate_macros::propagate;
|
||||
|
||||
#[propagate]
|
||||
const fn invalid() -> Result<(), ()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: kaptan_err_propagate::propagate does not support const functions
|
||||
--> tests/ui/const_fn.rs:4:1
|
||||
|
|
||||
4 | const fn invalid() -> Result<(), ()> {
|
||||
| ^^^^^
|
||||
@@ -0,0 +1,13 @@
|
||||
extern crate self as kaptan_err_propagate;
|
||||
|
||||
use kaptan_err_propagate_macros::KaptanError;
|
||||
|
||||
#[derive(Debug, KaptanError)]
|
||||
enum DuplicateId {
|
||||
#[kaptan_error(id = 42)]
|
||||
First,
|
||||
#[kaptan_error(id = 42)]
|
||||
Second,
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: Kaptan error ID 42 is already assigned to variant First
|
||||
--> tests/ui/duplicate_id.rs:10:5
|
||||
|
|
||||
10 | Second,
|
||||
| ^^^^^^
|
||||
@@ -0,0 +1,37 @@
|
||||
extern crate self as kaptan_err_propagate;
|
||||
|
||||
use kaptan_err_propagate_macros::KaptanError;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct ErrorId(u16);
|
||||
|
||||
impl ErrorId {
|
||||
pub const fn new(id: u16) -> Self {
|
||||
Self(id)
|
||||
}
|
||||
|
||||
pub const fn get(self) -> u16 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ErrorReport {
|
||||
fn error_id(&self) -> ErrorId;
|
||||
}
|
||||
|
||||
#[derive(Debug, KaptanError)]
|
||||
enum FirstError {
|
||||
#[kaptan_error(id = 77)]
|
||||
Failed,
|
||||
}
|
||||
|
||||
#[derive(Debug, KaptanError)]
|
||||
enum SecondError {
|
||||
#[kaptan_error(id = 77)]
|
||||
Failed,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _ = FirstError::Failed.error_id();
|
||||
let _ = SecondError::Failed.error_id();
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
error: symbol `__kaptan_error_id_77` is already defined
|
||||
--> tests/ui/duplicate_id_across_enums.rs:28:17
|
||||
|
|
||||
28 | #[derive(Debug, KaptanError)]
|
||||
| ^^^^^^^^^^^
|
||||
|
|
||||
= note: this error originates in the derive macro `KaptanError` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
@@ -0,0 +1,43 @@
|
||||
extern crate self as kaptan_err_propagate;
|
||||
|
||||
use kaptan_err_propagate_macros::KaptanError;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct ErrorId(u16);
|
||||
|
||||
impl ErrorId {
|
||||
pub const fn new(id: u16) -> Self {
|
||||
Self(id)
|
||||
}
|
||||
|
||||
pub const fn get(self) -> u16 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ErrorReport {
|
||||
fn error_id(&self) -> ErrorId;
|
||||
}
|
||||
|
||||
#[derive(Debug, KaptanError)]
|
||||
enum Generic<'a, T>
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
#[kaptan_error(id = 65201)]
|
||||
Unit,
|
||||
#[kaptan_error(id = 65202)]
|
||||
Tuple(T, u8),
|
||||
#[kaptan_error(id = 65203)]
|
||||
Named { label: &'a str },
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn assert_error<T: core::error::Error>() {}
|
||||
assert_error::<Generic<'_, u8>>();
|
||||
assert_eq!(Generic::<u8>::Unit.error_id().get(), 65201);
|
||||
assert_eq!(Generic::Tuple(1_u8, 2).error_id().get(), 65202);
|
||||
let named: Generic<'_, u8> = Generic::Named { label: "test" };
|
||||
assert_eq!(named.error_id().get(), 65203);
|
||||
assert_eq!(named.to_string(), "65203");
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
extern crate self as kaptan_err_propagate;
|
||||
|
||||
use kaptan_err_propagate_macros::KaptanError;
|
||||
|
||||
#[derive(Debug, KaptanError)]
|
||||
enum MissingId {
|
||||
Variant,
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: missing `#[kaptan_error(id = ...)]` on Kaptan error variant
|
||||
--> tests/ui/missing_id.rs:7:5
|
||||
|
|
||||
7 | Variant,
|
||||
| ^^^^^^^
|
||||
@@ -0,0 +1,6 @@
|
||||
use kaptan_err_propagate_macros::propagate;
|
||||
|
||||
#[propagate]
|
||||
fn invalid() {}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,7 @@
|
||||
error: kaptan_err_propagate::propagate requires an explicit Result<T, E> return type
|
||||
--> tests/ui/missing_return.rs:3:1
|
||||
|
|
||||
3 | #[propagate]
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= note: this error originates in the attribute macro `propagate` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
@@ -0,0 +1,18 @@
|
||||
use kaptan_err_propagate_macros::propagate;
|
||||
|
||||
extern crate self as kaptan_err_propagate;
|
||||
|
||||
pub trait ErrorReport {}
|
||||
|
||||
pub mod __private {
|
||||
pub fn report_result<T, E: crate::ErrorReport>(_: &Result<T, E>, _: &'static str) {}
|
||||
}
|
||||
|
||||
struct PlainError;
|
||||
|
||||
#[propagate]
|
||||
fn invalid() -> Result<(), PlainError> {
|
||||
Err(PlainError)
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,17 @@
|
||||
error[E0277]: the trait bound `PlainError: ErrorReport` is not satisfied
|
||||
--> tests/ui/no_error_report.rs:13:1
|
||||
|
|
||||
13 | #[propagate]
|
||||
| ^^^^^^^^^^^^ the trait `ErrorReport` is not implemented for `PlainError`
|
||||
|
|
||||
help: this trait has no implementations, consider adding one
|
||||
--> tests/ui/no_error_report.rs:5:1
|
||||
|
|
||||
5 | pub trait ErrorReport {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
note: required by a bound in `report_result`
|
||||
--> tests/ui/no_error_report.rs:8:32
|
||||
|
|
||||
8 | pub fn report_result<T, E: crate::ErrorReport>(_: &Result<T, E>, _: &'static str) {}
|
||||
| ^^^^^^^^^^^^^^^^^^ required by this bound in `report_result`
|
||||
= note: this error originates in the attribute macro `propagate` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
@@ -0,0 +1,14 @@
|
||||
use kaptan_err_propagate_macros::propagate;
|
||||
|
||||
extern crate self as kaptan_err_propagate;
|
||||
|
||||
pub mod __private {
|
||||
pub fn report_result<T, E>(_: &Result<T, E>, _: &'static str) {}
|
||||
}
|
||||
|
||||
#[propagate]
|
||||
fn invalid() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,17 @@
|
||||
error[E0308]: mismatched types
|
||||
--> tests/ui/non_result.rs:9:1
|
||||
|
|
||||
9 | #[propagate]
|
||||
| ^^^^^^^^^^^^
|
||||
| |
|
||||
| expected `&Result<_, _>`, found `&bool`
|
||||
| arguments to this function are incorrect
|
||||
|
|
||||
= note: expected reference `&Result<_, _>`
|
||||
found reference `&bool`
|
||||
note: function defined here
|
||||
--> tests/ui/non_result.rs:6:12
|
||||
|
|
||||
6 | pub fn report_result<T, E>(_: &Result<T, E>, _: &'static str) {}
|
||||
| ^^^^^^^^^^^^^ ----------------
|
||||
= note: this error originates in the attribute macro `propagate` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
@@ -0,0 +1,8 @@
|
||||
use kaptan_err_propagate_macros::KaptanError;
|
||||
|
||||
#[derive(KaptanError)]
|
||||
struct NotAnError {
|
||||
code: u8,
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: KaptanError can only be derived for enums, not structs
|
||||
--> tests/ui/struct.rs:4:8
|
||||
|
|
||||
4 | struct NotAnError {
|
||||
| ^^^^^^^^^^
|
||||
@@ -0,0 +1,8 @@
|
||||
use kaptan_err_propagate_macros::KaptanError;
|
||||
|
||||
#[derive(KaptanError)]
|
||||
union NotAnError {
|
||||
code: u8,
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: KaptanError can only be derived for enums, not unions
|
||||
--> tests/ui/union.rs:4:7
|
||||
|
|
||||
4 | union NotAnError {
|
||||
| ^^^^^^^^^^
|
||||
@@ -0,0 +1,8 @@
|
||||
use kaptan_err_propagate_macros::propagate;
|
||||
|
||||
#[propagate]
|
||||
unsafe fn invalid() -> Result<(), ()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: kaptan_err_propagate::propagate does not support unsafe functions
|
||||
--> tests/ui/unsafe_fn.rs:4:1
|
||||
|
|
||||
4 | unsafe fn invalid() -> Result<(), ()> {
|
||||
| ^^^^^^
|
||||
@@ -0,0 +1,11 @@
|
||||
extern crate self as kaptan_err_propagate;
|
||||
|
||||
use kaptan_err_propagate_macros::KaptanError;
|
||||
|
||||
#[derive(Debug, KaptanError)]
|
||||
enum ZeroId {
|
||||
#[kaptan_error(id = 0)]
|
||||
Variant,
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: Kaptan error ID zero is reserved
|
||||
--> tests/ui/zero_id.rs:7:25
|
||||
|
|
||||
7 | #[kaptan_error(id = 0)]
|
||||
| ^
|
||||
Reference in new issue
Block a user