-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- use thiserror to make custom errors
- Loading branch information
Showing
3 changed files
with
38 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,22 @@ | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) 2022-2025 Andriel Ferreira <https://github.com/AndrielFR> | ||
|
||
use std::error::Error as StdError; | ||
use std::fmt; | ||
use thiserror::Error as TError; | ||
|
||
/// A specialized `Result` type for operations that can return an `Error`. | ||
/// | ||
/// This is defined as a convenience to avoid writing out `std::result::Result` | ||
/// with the `Error` type repeatedly. | ||
pub type Result<T> = std::result::Result<T, Error>; | ||
|
||
#[derive(Debug)] | ||
/// Represents the various errors that can occur in the application. | ||
/// | ||
/// This enum defines different types of errors that can be encountered, | ||
/// such as invalid IDs and API errors. | ||
#[derive(TError, Debug)] | ||
pub enum Error { | ||
#[error("invalid ID")] | ||
InvalidId, | ||
InvalidMediaType, | ||
#[error("api error: `{0}`")] | ||
ApiError(String), | ||
} | ||
|
||
#[derive(Debug)] | ||
struct InvalidId; | ||
|
||
impl StdError for InvalidId {} | ||
|
||
impl fmt::Display for InvalidId { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "The given id is invalid") | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct InvalidMediaType; | ||
|
||
impl StdError for InvalidMediaType {} | ||
|
||
impl fmt::Display for InvalidMediaType { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "The given media type is invalid") | ||
} | ||
} |