Skip to content

Commit 7bcd0b3

Browse files
committed
errors: Remove ChainError impl for Option
For `Option` there is nothing to chain onto, so we might as well use the built-in `ok_or_else()` method to convert the `Option` to a `Result`
1 parent 5f13e07 commit 7bcd0b3

File tree

4 files changed

+5
-19
lines changed

4 files changed

+5
-19
lines changed

src/controllers/krate/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ pub fn publish(req: &mut dyn RequestExt) -> EndpointResult {
130130

131131
let content_length = req
132132
.content_length()
133-
.chain_error(|| cargo_err("missing header: Content-Length"))?;
133+
.ok_or_else(|| cargo_err("missing header: Content-Length"))?;
134134

135135
let maximums = Maximums::new(
136136
krate.max_upload_size,

src/controllers/krate/search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::models::{
1010
Crate, CrateBadge, CrateOwner, CrateVersions, OwnerKind, TopVersions, Version,
1111
};
1212
use crate::schema::*;
13-
use crate::util::errors::{bad_request, ChainError};
13+
use crate::util::errors::bad_request;
1414
use crate::views::EncodableCrate;
1515

1616
use crate::controllers::helpers::pagination::{Page, Paginated, PaginationOptions};
@@ -153,7 +153,7 @@ pub fn search(req: &mut dyn RequestExt) -> EndpointResult {
153153
letter
154154
.chars()
155155
.next()
156-
.chain_error(|| bad_request("letter value must contain 1 character"))?
156+
.ok_or_else(|| bad_request("letter value must contain 1 character"))?
157157
.to_lowercase()
158158
.collect::<String>()
159159
);

src/controllers/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn new(req: &mut dyn RequestExt) -> EndpointResult {
3939
let max_size = 2000;
4040
let length = req
4141
.content_length()
42-
.chain_error(|| bad_request("missing header: Content-Length"))?;
42+
.ok_or_else(|| bad_request("missing header: Content-Length"))?;
4343

4444
if length > max_size {
4545
return Err(bad_request(&format!("max content length is: {}", max_size)));

src/util/errors.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,6 @@ impl<T, E: AppError> ChainError<T> for Result<T, E> {
175175
}
176176
}
177177

178-
impl<T> ChainError<T> for Option<T> {
179-
fn chain_error<E, C>(self, callback: C) -> AppResult<T>
180-
where
181-
E: AppError,
182-
C: FnOnce() -> E,
183-
{
184-
match self {
185-
Some(t) => Ok(t),
186-
None => Err(Box::new(callback())),
187-
}
188-
}
189-
}
190-
191178
impl<E: AppError> AppError for ChainedError<E> {
192179
fn response(&self) -> Option<AppResponse> {
193180
self.error.response()
@@ -274,8 +261,7 @@ pub(crate) fn std_error(e: Box<dyn AppError>) -> Box<dyn Error + Send> {
274261
#[test]
275262
fn chain_error_internal() {
276263
assert_eq!(
277-
None::<()>
278-
.chain_error(|| internal("inner"))
264+
Err::<(), _>(internal("inner"))
279265
.chain_error(|| internal("middle"))
280266
.chain_error(|| internal("outer"))
281267
.unwrap_err()

0 commit comments

Comments
 (0)