#mismatched types expected '&str', found struct 'stdstringString'

14 messages · Page 1 of 1 (latest)

clear cypress
#

returning (StatusCode, String) instead would fix this

#

but it would be much better to return an actual error type

#

you could do Cow<'static str> if you want to support both

indigo jasper
clear cypress
#

could use Cow

#

but this error is pretty sub optimal

#

designing an error type would be faster, easier and far less error prone

indigo jasper
#

how could I do that ? I use Validator to validate my input, and customs errors for the rest

clear cypress
#

what is Validate's error type? is it something from a crate you're using?

indigo jasper
#

yes

clear cypress
#

at its simplest, you can just wrap it.

#

for example: rust struct MyError{ BadRequest(ValidationError), InternalError(Box<dyn Error>), }

#

and then you could write (assuming you write some minor boilerplate like implementing Error and some From conversions: ```rust
#[axum_macros::debug_handler]
pub async fn update_task(
Path(id): Path<i32>,
Extension(database_conn): Extension<DatabaseConnection>,
Json(request): Json<TaskRequest>,
) -> Result<Json<TaskRequest>, MyError> {

request.validate()?;

let task: Option<tasks::Model> = tasks::Entity::find_by_id(id)
    .one(&database_conn)
    .await
    .map_err(MyError::InternalError)?;```