mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-12-18 22:16:40 +00:00
Use Result
This commit is contained in:
parent
1c27412280
commit
56ee4e7746
2 changed files with 3 additions and 3 deletions
|
@ -11,7 +11,7 @@ impl SubscriberName {
|
||||||
/// Returns an instance of `SubscriberName` if the input satisfies all
|
/// Returns an instance of `SubscriberName` if the input satisfies all
|
||||||
/// our validation constraints on subscriber names.
|
/// our validation constraints on subscriber names.
|
||||||
/// It panics otherwise.
|
/// It panics otherwise.
|
||||||
pub fn parse(s: String) -> SubscriberName {
|
pub fn parse(s: String) -> Result<SubscriberName, String> {
|
||||||
// `.trim()` returns a view over the input `s` without trailing
|
// `.trim()` returns a view over the input `s` without trailing
|
||||||
// whitespace-like characters.
|
// whitespace-like characters.
|
||||||
// `.is_empty` checks if the view contains any character.
|
// `.is_empty` checks if the view contains any character.
|
||||||
|
@ -38,7 +38,7 @@ impl SubscriberName {
|
||||||
if is_empty_or_whitespace || is_too_long || contains_forbidden_characters {
|
if is_empty_or_whitespace || is_too_long || contains_forbidden_characters {
|
||||||
panic!(format!("{} is not a valid subscriber name.", s))
|
panic!(format!("{} is not a valid subscriber name.", s))
|
||||||
} else {
|
} else {
|
||||||
Self(s)
|
Ok(Self(s))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ pub async fn subscribe(
|
||||||
) -> Result<HttpResponse, HttpResponse> {
|
) -> Result<HttpResponse, HttpResponse> {
|
||||||
let new_subscriber = NewSubscriber {
|
let new_subscriber = NewSubscriber {
|
||||||
email: form.0.email,
|
email: form.0.email,
|
||||||
name: SubscriberName::parse(form.0.name),
|
name: SubscriberName::parse(form.0.name).expect("Name validation failed."),
|
||||||
};
|
};
|
||||||
insert_subscriber(&pool, &new_subscriber)
|
insert_subscriber(&pool, &new_subscriber)
|
||||||
.await
|
.await
|
||||||
|
|
Loading…
Reference in a new issue