All checks were successful
actionlint check / actionlint check (pull_request) Successful in 7s
conventional pull request title check / conventional pull request title check (pull_request) Successful in 3s
GitLeaks check / GitLeaks check (pull_request) Successful in 14s
hadolint check / hadolint check (pull_request) Successful in 15s
conventional commit messages check / conventional commit messages check (pull_request) Successful in 7s
dotenv-linter check / dotenv-linter check (pull_request) Successful in 8s
htmlhint check / htmlhint check (pull_request) Successful in 20s
markdownlint check / markdownlint check (pull_request) Successful in 32s
Prettier check / Prettier check (pull_request) Successful in 33s
checkov check / checkov check (pull_request) Successful in 1m20s
ShellCheck check / ShellCheck check (pull_request) Successful in 30s
Stylelint check / Stylelint check (pull_request) Successful in 27s
yamllint check / yamllint check (pull_request) Successful in 26s
Rust check / Rust check (pull_request) Successful in 12m52s
80 lines
2.6 KiB
Rust
80 lines
2.6 KiB
Rust
use crate::errors::error::Error;
|
|
use crate::errors::error_vec::ErrorVec;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::fmt::Display;
|
|
use std::str::FromStr;
|
|
use validator::{ValidationErrors, ValidationErrorsKind};
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub enum SubtaskError {
|
|
TitleLengthInvalid,
|
|
TaskNotFound,
|
|
Error(Error),
|
|
}
|
|
|
|
impl From<ValidationErrors> for ErrorVec<SubtaskError> {
|
|
fn from(validation_errors: ValidationErrors) -> Self {
|
|
validation_errors
|
|
.errors()
|
|
.iter()
|
|
.flat_map(|(field, error_kind)| match field.as_ref() {
|
|
"title" => match error_kind {
|
|
ValidationErrorsKind::Field(validation_errors) => validation_errors
|
|
.iter()
|
|
.map(|validation_error| validation_error.code.as_ref())
|
|
.map(|code| match code {
|
|
"title_length" => SubtaskError::TitleLengthInvalid,
|
|
_ => panic!("Unexpected validation error code: `{code}`."),
|
|
})
|
|
.collect::<Vec<SubtaskError>>(),
|
|
_ => panic!("Unexpected validation error kind."),
|
|
},
|
|
_ => panic!("Unexpected validation field name: `{field}`."),
|
|
})
|
|
.collect::<Vec<SubtaskError>>()
|
|
.into()
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "server")]
|
|
impl From<diesel::result::Error> for SubtaskError {
|
|
fn from(diesel_error: diesel::result::Error) -> Self {
|
|
match diesel_error {
|
|
diesel::result::Error::DatabaseError(
|
|
diesel::result::DatabaseErrorKind::ForeignKeyViolation,
|
|
info,
|
|
) => match info.constraint_name() {
|
|
Some("subtasks_task_id_fkey") => Self::TaskNotFound,
|
|
_ => Self::Error(Error::ServerInternal),
|
|
},
|
|
_ => Self::Error(Error::ServerInternal),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<ErrorVec<Error>> for ErrorVec<SubtaskError> {
|
|
fn from(error_vec: ErrorVec<Error>) -> Self {
|
|
Vec::from(error_vec)
|
|
.into_iter()
|
|
.map(SubtaskError::Error)
|
|
.collect::<Vec<SubtaskError>>()
|
|
.into()
|
|
}
|
|
}
|
|
|
|
// Has to be implemented for Dioxus server functions.
|
|
impl Display for SubtaskError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{self:?}")
|
|
}
|
|
}
|
|
|
|
// Has to be implemented for Dioxus server functions.
|
|
impl FromStr for SubtaskError {
|
|
type Err = ();
|
|
|
|
fn from_str(_: &str) -> Result<Self, Self::Err> {
|
|
Ok(Self::Error(Error::ServerInternal))
|
|
}
|
|
}
|