pocketbase_rs/
error.rs

1//! Various errors module.
2
3use core::fmt;
4use std::collections::HashMap;
5
6use serde::Deserialize;
7use thiserror::Error;
8
9pub use crate::records::auth::auth_with_password::AuthenticationError;
10pub use crate::records::auth::impersonate::ImpersonateError;
11pub use crate::records::crud::create::CreateError;
12pub use crate::records::crud::update::UpdateError;
13
14/// This error represents the error returned by the `PocketBase`
15/// instance in case of a 400 error.
16#[derive(Deserialize, Debug)]
17pub struct BadRequestResponse {
18    /// HTTP Status Code.
19    pub status: u16,
20    /// Description from given by `PocketBase` about why the error happened.
21    pub message: String,
22    /// A list of fields that caused the error.
23    pub data: HashMap<String, BadRequestField>,
24}
25
26/// Represents an instance of one of the errors that could be returned on a bad request.
27///
28/// This struct holds detailed information about a single validation error,
29/// including the field name, error code, and a user-friendly message.
30#[derive(Deserialize, Debug)]
31pub struct BadRequestError {
32    /// Name of the field.
33    pub name: String,
34    /// Error code.
35    pub code: String,
36    /// More details about the error.
37    pub message: String,
38}
39
40impl fmt::Display for BadRequestError {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        write!(f, "{}: {} {}", self.name, self.code, self.message)
43    }
44}
45
46/// Represents one of the fields that caused the Bad Request error.
47#[derive(Deserialize, Debug)]
48pub struct BadRequestField {
49    /// Error code *(example: `validation_required`)*.
50    pub code: String,
51    /// A text explaining in a readable way what this error is.
52    pub message: String,
53}
54
55/// Represents errors when interacting with the `PocketBase` API.
56///
57/// This enum provides a set of error types that may occur during
58/// API requests, each indicating a specific issue encountered.
59#[derive(Error, Debug)]
60pub enum RequestError {
61    /// Communication with the `PocketBase` API was successful,
62    /// but returned a [400 Bad Request]("https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400") HTTP error response.
63    ///
64    /// Your request may be missing fields or its content doesn't match what `PocketBase` expects to receive.
65    #[error("Bad Request: Something went wrong while processing your request. {0}")]
66    BadRequest(String),
67    /// Communication with the `PocketBase` API was successful,
68    /// but returned a [401 Unauthorized]("https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401") HTTP error response.
69    ///
70    /// The request may require an Authorization Token.
71    #[error("Unauthorized: The request may require an Authorization Token.")]
72    Unauthorized,
73    /// Communication with the `PocketBase` API was successful,
74    /// but returned a [403 Forbidden]("https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403") HTTP error response.
75    ///
76    /// The authenticated user may not have permissions for this interaction.
77    #[error("Forbidden: The authenticated user may not have permissions for this interaction.")]
78    Forbidden,
79    /// Communication with the `PocketBase` API was successful,
80    /// but returned a [404 Not Found]("https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404") HTTP error response.
81    #[error("Not Found: The requested resource could not be found.")]
82    NotFound,
83    /// The response could not be parsed into the expected data structure.
84    #[error(
85        "Parse Error: Could not parse response into the expected data structure. It usually means that there is a missmatch between the provided Generic Type Parameter and your Collection definition. - {0}"
86    )]
87    ParseError(String),
88    /// The `PocketBase` API interaction timed out. It may be offline.
89    #[error(
90        "Unreachable: The PocketBase API interaction timed out, or the service may be offline."
91    )]
92    Unreachable,
93    /// Too many requests were sent to the API.
94    ///
95    /// The server is rate limiting requests. Wait before retrying.
96    #[error(
97        "Too Many Requests: The server is rate limiting requests. Please wait before retrying."
98    )]
99    TooManyRequests,
100    /// Unhandled error.
101    ///
102    /// Usually emitted when something unexpected happened, and isn't handled correctly by this crate.
103    #[error("Unhandled Error: An unexpected error occurred.")]
104    Unhandled,
105}