Skip to content

Commit b1cb2a7

Browse files
committed
Send some json back on a 403 unauthorized
1 parent ab18c44 commit b1cb2a7

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/util/errors.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use git2;
1111

1212
use util::json_response;
1313

14+
#[deriving(Encodable)] struct Error { detail: String }
15+
#[deriving(Encodable)] struct Bad { errors: Vec<Error> }
16+
1417
pub trait CargoError: Send {
1518
fn description(&self) -> String;
1619
fn detail(&self) -> Option<String> { None }
@@ -40,9 +43,6 @@ pub trait CargoError: Send {
4043
}
4144

4245
fn response(&self) -> Option<Response> {
43-
#[deriving(Encodable)] struct Error { detail: String }
44-
#[deriving(Encodable)] struct Bad { errors: Vec<Error> }
45-
4646
if self.human() {
4747
Some(json_response(&Bad {
4848
errors: vec![Error { detail: self.description() }]
@@ -242,11 +242,13 @@ impl CargoError for Unauthorized {
242242
fn description(&self) -> String { "unauthorized".to_string() }
243243

244244
fn response(&self) -> Option<Response> {
245-
Some(Response {
246-
status: (403, "Forbidden"),
247-
headers: HashMap::new(),
248-
body: box MemReader::new(Vec::new()),
249-
})
245+
let mut response = json_response(&Bad {
246+
errors: vec![Error {
247+
detail: "must be logged in to perform that action".to_string(),
248+
}],
249+
});
250+
response.status = (403, "Forbidden");
251+
return Some(response);
250252
}
251253
}
252254

0 commit comments

Comments
 (0)