Skip to main content

kagi_sdk/
error.rs

1use thiserror::Error;
2
3use crate::{
4    auth::CredentialKind,
5    routing::{EndpointId, ParserShape, ProtocolSurface},
6};
7
8#[derive(Debug, Error)]
9pub enum KagiError {
10    #[error("invalid {kind} credential: {reason}")]
11    InvalidCredential {
12        kind: CredentialKind,
13        reason: String,
14    },
15
16    #[error("missing credential configuration: {reason}")]
17    MissingCredentialConfiguration { reason: String },
18
19    #[error(
20        "conflicting credential configuration: attempted to set {attempted} after {already_set}"
21    )]
22    ConflictingCredentialConfiguration {
23        already_set: CredentialKind,
24        attempted: CredentialKind,
25    },
26
27    #[error("invalid input for `{field}`: {reason}")]
28    InvalidInput { field: &'static str, reason: String },
29
30    #[error("invalid client configuration: {reason}")]
31    InvalidClientConfiguration { reason: String },
32
33    #[error(
34        "unsupported credential/surface combination: {credential} cannot access {surface}; expected {expected}"
35    )]
36    UnsupportedAuthSurface {
37        surface: ProtocolSurface,
38        credential: CredentialKind,
39        expected: CredentialKind,
40    },
41
42    #[error("unsupported capability for {endpoint}: provided {credential}, expected {expected}")]
43    UnsupportedCapability {
44        endpoint: EndpointId,
45        credential: CredentialKind,
46        expected: CredentialKind,
47    },
48
49    #[error("network transport failed for {endpoint}: {source}")]
50    Transport {
51        endpoint: EndpointId,
52        #[source]
53        source: reqwest::Error,
54    },
55
56    #[error("failed to parse {parser:?} response for {endpoint}: {reason}")]
57    ResponseParse {
58        endpoint: EndpointId,
59        parser: ParserShape,
60        reason: String,
61    },
62
63    #[error("bot token unauthorized for {endpoint}: {message}")]
64    UnauthorizedBotToken {
65        endpoint: EndpointId,
66        message: String,
67    },
68
69    #[error("session token invalid or expired for {endpoint} (HTTP {status}): {message}")]
70    InvalidSession {
71        endpoint: EndpointId,
72        status: u16,
73        message: String,
74    },
75
76    #[error("kagi API failure for {endpoint} (HTTP {status}, code {code:?}): {message}")]
77    ApiFailure {
78        endpoint: EndpointId,
79        status: u16,
80        code: Option<String>,
81        message: String,
82    },
83}