1
+ use once_cell:: sync:: Lazy ;
1
2
use std:: fmt:: Debug ;
2
3
3
4
use nanoid:: nanoid;
@@ -17,18 +18,6 @@ pub enum ErrorOrPanic<'a> {
17
18
Panic ( & ' a Option < String > ) ,
18
19
}
19
20
20
- impl < ' a > From < & ' a color_eyre:: eyre:: Error > for ErrorOrPanic < ' a > {
21
- fn from ( val : & ' a color_eyre:: eyre:: Error ) -> Self {
22
- ErrorOrPanic :: Error ( val)
23
- }
24
- }
25
-
26
- impl < ' a > From < & ' a Option < String > > for ErrorOrPanic < ' a > {
27
- fn from ( val : & ' a Option < String > ) -> Self {
28
- ErrorOrPanic :: Panic ( val)
29
- }
30
- }
31
-
32
21
impl ErrorOrPanic < ' _ > {
33
22
/// Return whether `self` is a panic or an error.
34
23
fn type_string ( & self ) -> String {
@@ -39,6 +28,12 @@ impl ErrorOrPanic<'_> {
39
28
}
40
29
}
41
30
31
+ pub static ERROR_LOGS_CHANNEL : Lazy < Option < ChannelId > > = Lazy :: new ( || {
32
+ std:: env:: var ( "ERROR_LOGS_CHANNEL" )
33
+ . ok ( )
34
+ . and_then ( |s| s. parse :: < ChannelId > ( ) . ok ( ) )
35
+ } ) ;
36
+
42
37
/// A wrapped type around errors or panics encapsulated in [`ErrorOrPanic`] that includes context from Poise and a randomly generated `error_id`.
43
38
#[ derive( Debug ) ]
44
39
pub struct ValfiskError < ' a > {
@@ -51,14 +46,21 @@ pub struct ValfiskError<'a> {
51
46
}
52
47
53
48
impl ValfiskError < ' _ > {
54
- /// Create a new [`ValfiskError`] from an error or a panic string and Poise context.
49
+ /// Create a new [`ValfiskError`] from an error and Poise context.
55
50
#[ must_use]
56
- pub fn new < ' a > (
57
- error_or_panic : impl Into < ErrorOrPanic < ' a > > ,
58
- ctx : & ' a Context ,
59
- ) -> ValfiskError < ' a > {
51
+ pub fn error < ' a > ( error : & ' a color_eyre:: eyre:: Error , ctx : & ' a Context ) -> ValfiskError < ' a > {
60
52
ValfiskError {
61
- error_or_panic : error_or_panic. into ( ) ,
53
+ error_or_panic : ErrorOrPanic :: Error ( error) ,
54
+ ctx,
55
+ error_id : nanoid ! ( 8 ) ,
56
+ }
57
+ }
58
+
59
+ /// Create a new [`ValfiskError`] from a panic string and Poise context.
60
+ #[ must_use]
61
+ pub fn panic < ' a > ( panic : & ' a Option < String > , ctx : & ' a Context ) -> ValfiskError < ' a > {
62
+ ValfiskError {
63
+ error_or_panic : ErrorOrPanic :: Panic ( panic) ,
62
64
ctx,
63
65
error_id : nanoid ! ( 8 ) ,
64
66
}
@@ -91,14 +93,7 @@ impl ValfiskError<'_> {
91
93
/// Report the error to a channel defined through the environment variable `ERROR_LOGS_CHANNEL`.
92
94
#[ tracing:: instrument( skip( self ) ) ]
93
95
pub async fn handle_report ( & self ) {
94
- if let Ok ( channel_id) = match std:: env:: var ( "ERROR_LOGS_CHANNEL" ) {
95
- Ok ( channel_id_str) => channel_id_str
96
- . parse :: < u64 > ( )
97
- . map_err ( color_eyre:: eyre:: Error :: from) ,
98
- Err ( err) => Err ( color_eyre:: eyre:: Error :: from ( err) ) ,
99
- } {
100
- let channel = ChannelId :: new ( channel_id) ;
101
-
96
+ if let Some ( channel) = * ERROR_LOGS_CHANNEL {
102
97
let embed = CreateEmbed :: default ( )
103
98
. title ( "An error occurred!" )
104
99
. description ( format ! ( "```\n {:#?}\n ```" , self . error_or_panic) )
0 commit comments