From cb404df6d8fe0913f3e3d63ccdb283d9828e6e2e Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Tue, 22 Dec 2015 01:45:21 -0500 Subject: [PATCH] Implement FromResponse for OAuth2Error --- src/error.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/error.rs b/src/error.rs index b7093fa..40e0416 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,6 +3,10 @@ use std::error::Error; use std::fmt; +use rustc_serialize::json::Json; + +use client::response::{FromResponse, ParseError, JsonHelper}; + /// OAuth 2.0 error codes. /// /// See [RFC 6749, section 5.2](http://tools.ietf.org/html/rfc6749#section-5.2). @@ -81,3 +85,20 @@ impl fmt::Display for OAuth2Error { impl Error for OAuth2Error { fn description(&self) -> &str { "OAuth 2.0 API error" } } + +impl FromResponse for OAuth2Error { + fn from_response(json: &Json) -> Result { + let json = JsonHelper(json); + let obj = try!(json.as_object()); + + let code = try!(obj.get_string("error")); + let description = obj.0.get("error_description").and_then(Json::as_string); + let uri = obj.0.get("error_uri").and_then(Json::as_string); + + Ok(OAuth2Error { + code: code.into(), + description: description.map(Into::into), + uri: uri.map(Into::into), + }) + } +}