Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some or more documentation about custom error handlers. #48

Open
DenuxPlays opened this issue Jan 19, 2024 · 0 comments
Open

Some or more documentation about custom error handlers. #48

DenuxPlays opened this issue Jan 19, 2024 · 0 comments

Comments

@DenuxPlays
Copy link

DenuxPlays commented Jan 19, 2024

I would love if there would be more documentation about error handlers available.
The only thing I found was this comment

For example I went with this:

use actix_web_validator::error::flatten_errors;
use serde::Serialize;
use serde_json::Value;
use std::collections::HashMap;

#[derive(Serialize)]
pub struct ValidationErrorJsonPayload {
	pub message: String,
	pub fields: Vec<FieldError>,
}

#[derive(Serialize)]
pub struct FieldError {
	pub field_name: String,
	pub params: HashMap<String, Value>,
}

impl From<&validator::ValidationErrors> for ValidationErrorJsonPayload {
	fn from(error: &validator::ValidationErrors) -> Self {
		let errors = flatten_errors(error);
		let mut field_errors: Vec<FieldError> = Vec::new();
		for (index, field, error) in errors {
			field_errors.insert(
				index as usize,
				FieldError {
					field_name: field,
					params: error.params.clone().into_iter().map(|(key, value)| (key.into_owned(), value)).collect(),
				},
			)
		}
		ValidationErrorJsonPayload {
			message: "Validation error".to_owned(),
			fields: field_errors,
		}
	}
}

WIth this setup the actual error is getting returned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant