Skip to content

Commit

Permalink
Better error messages for errors from Rocket.Chat
Browse files Browse the repository at this point in the history
  • Loading branch information
exul committed Mar 19, 2017
1 parent ae54b2a commit 81adfff
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
7 changes: 1 addition & 6 deletions src/matrix-rocketchat/api/rocketchat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ impl RocketchatApi {
let url = base_url.clone() + "/api/info";
let params = HashMap::new();

debug!(logger, format!("Querying Rocket.Chat server {} for API versions", url));

let (body, status_code) = match RestApi::call(Method::Get, &url, "", &params, None) {
Ok((body, status_code)) => (body, status_code),
Err(err) => {
Expand All @@ -102,13 +100,10 @@ impl RocketchatApi {
match serde_json::from_str(&body).chain_err(|| ErrorKind::NoRocketchatServer(url.clone())) {
Ok(rocketchat_info) => rocketchat_info,
Err(err) => {
bail_error!(err,
t!(["errors", "no_rocketchat_server"]).with_vars(vec![("rocketchat_url", url)]));
bail_error!(err, t!(["errors", "no_rocketchat_server"]).with_vars(vec![("rocketchat_url", url)]));
}
};

debug!(logger, format!("Rocket.Chat version {:?}", rocketchat_info.version));

RocketchatApi::get_max_supported_version_api(rocketchat_info.version, base_url, access_token, logger)
}

Expand Down
14 changes: 12 additions & 2 deletions src/matrix-rocketchat/api/rocketchat/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub struct PostChatMessageEndpoint<'a> {
/// Payload of the post chat message endpoint
#[derive(Serialize)]
pub struct PostChatMessagePayload<'a> {
#[serde(rename = "roomId")]
room_id: &'a str,
text: Option<&'a str>,
}
Expand Down Expand Up @@ -203,6 +204,8 @@ impl RocketchatApi {

impl super::RocketchatApi for RocketchatApi {
fn login(&self, username: &str, password: &str) -> Result<(String, String)> {
debug!(self.logger, format!("Logging in user with username {} on Rocket.Chat server {}", username, &self.base_url));

let login_endpoint = LoginEndpoint {
base_url: self.base_url.clone(),
payload: LoginPayload {
Expand All @@ -224,6 +227,8 @@ impl super::RocketchatApi for RocketchatApi {
}

fn username(&self, user_id: String, auth_token: String) -> Result<String> {
debug!(self.logger, format!("Querying username for user_id {} on Rocket.Chat server {}", user_id, &self.base_url));

let me_endpoint = MeEndpoint {
base_url: self.base_url.clone(),
user_id: user_id,
Expand All @@ -244,6 +249,8 @@ impl super::RocketchatApi for RocketchatApi {
}

fn channels_list(&self, user_id: String, auth_token: String) -> Result<Vec<Channel>> {
debug!(self.logger, format!("Getting channel list from Rocket.Chat server {}", &self.base_url));

let channels_list_endpoint = ChannelsListEndpoint {
base_url: self.base_url.clone(),
user_id: user_id,
Expand All @@ -265,6 +272,8 @@ impl super::RocketchatApi for RocketchatApi {
}

fn post_chat_message(&self, user_id: String, auth_token: String, text: &str, room_id: &str) -> Result<()> {
debug!(self.logger, format!("Forwarding message to to Rocket.Chat room {}", room_id));

let post_chat_message_endpoint = PostChatMessageEndpoint {
base_url: self.base_url.clone(),
user_id: user_id,
Expand Down Expand Up @@ -300,10 +309,11 @@ fn build_error(endpoint: String, body: &str, status_code: &StatusCode) -> Error

if *status_code == StatusCode::Unauthorized {
return Error {
error_chain: ErrorKind::AuthenticationFailed(rocketchat_error_resp.message).into(),
error_chain: ErrorKind::AuthenticationFailed(rocketchat_error_resp.message.unwrap_or_default()).into(),
user_message: Some(t!(["errors", "authentication_failed"])),
};
}

Error::from(ErrorKind::RocketchatError(rocketchat_error_resp.message))
let msg = rocketchat_error_resp.message.unwrap_or(rocketchat_error_resp.error.unwrap_or(body.to_string()));
Error::from(ErrorKind::RocketchatError(msg))
}
6 changes: 4 additions & 2 deletions src/matrix-rocketchat/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ pub struct MatrixErrorResponse {
#[derive(Deserialize, Serialize)]
pub struct RocketchatErrorResponse {
/// Status returned by the Rocket.Chat API
pub status: String,
pub status: Option<String>,
/// Error message returned by the Rocket.Chat API
pub message: String,
pub message: Option<String>,
/// The error that occured
pub error: Option<String>,
}

#[derive(Debug)]
Expand Down
5 changes: 3 additions & 2 deletions tests/matrix-rocketchat-test/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ pub struct RocketchatErrorResponder {
impl Handler for RocketchatErrorResponder {
fn handle(&self, _request: &mut Request) -> IronResult<Response> {
let error_response = RocketchatErrorResponse {
status: "error".to_string(),
message: self.message.clone(),
status: Some("error".to_string()),
message: Some(self.message.clone()),
error: None,
};
let payload = serde_json::to_string(&error_response).unwrap();
Ok(Response::with((self.status, payload)))
Expand Down

0 comments on commit 81adfff

Please sign in to comment.