twilight_interactions/
error.rs1use std::{
4 error::Error,
5 fmt::{Display, Formatter, Result as FmtResult},
6};
7
8use twilight_model::{application::command::CommandOptionType, channel::ChannelType};
9
10#[derive(Debug, Clone, PartialEq)]
17pub enum ParseError {
18 EmptyOptions,
22 Option(ParseOptionError),
24}
25
26impl Error for ParseError {}
27
28impl Display for ParseError {
29 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
30 match self {
31 ParseError::EmptyOptions => write!(f, "received an empty option list"),
32 ParseError::Option(error) => error.fmt(f),
33 }
34 }
35}
36
37#[derive(Debug, Clone, PartialEq)]
41pub struct ParseOptionError {
42 pub field: String,
44 pub kind: ParseOptionErrorType,
46}
47
48impl Error for ParseOptionError {}
49
50impl Display for ParseOptionError {
51 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
52 write!(f, "failed to parse option `{}`: ", self.field)?;
53
54 match &self.kind {
55 ParseOptionErrorType::InvalidType(ty) => write!(f, "invalid type, found {}", ty.kind()),
56 ParseOptionErrorType::InvalidChoice(choice) => {
57 write!(f, "invalid choice value, found `{choice}`")
58 }
59 ParseOptionErrorType::IntegerOutOfRange(val) => {
60 write!(f, "out of range integer, received `{val}`")
61 }
62 ParseOptionErrorType::NumberOutOfRange(val) => {
63 write!(f, "out of range number, received `{val}`")
64 }
65 ParseOptionErrorType::StringLengthOutOfRange(val) => {
66 write!(f, "out of range string length, received `{val}`")
67 }
68 ParseOptionErrorType::InvalidChannelType(kind) => {
69 write!(f, "invalid channel type, received `{}`", kind.name())
70 }
71 ParseOptionErrorType::LookupFailed(id) => write!(f, "failed to resolve `{id}`"),
72 ParseOptionErrorType::UnknownField => write!(f, "unknown field"),
73 ParseOptionErrorType::UnknownSubcommand => write!(f, "unknown subcommand"),
74 ParseOptionErrorType::RequiredField => write!(f, "missing required field"),
75 }
76 }
77}
78
79#[derive(Debug, Clone, PartialEq)]
81pub enum ParseOptionErrorType {
82 InvalidType(CommandOptionType),
84 InvalidChoice(String),
86 IntegerOutOfRange(i64),
88 NumberOutOfRange(f64),
90 StringLengthOutOfRange(String),
92 InvalidChannelType(ChannelType),
94 LookupFailed(u64),
96 RequiredField,
98 UnknownField,
100 UnknownSubcommand,
102}