public static Exception WebExceptionHelper(WebException exception, string scopeRequired = null) { var errorMessage = ""; var errorType = ""; try { var bodyText = exception.GetResponseBody(); errorMessage = JsonObject.Parse(bodyText).Object("error").Get("message"); errorType = JsonObject.Parse(bodyText).Object("error").Get("type"); if (errorMessage.IsEmpty()) errorMessage = bodyText; } catch { } if (!errorMessage.IsEmpty()) { switch (exception.GetStatusCode()) { case 404: if (errorMessage.Contains("Room not found")) return new HipchatRoomNotFoundException(errorMessage, exception); break; } return new HipchatWebException("\nMessage: '{0}'\nType: '{1}'".Fmt(errorMessage, errorType), exception); } if (exception.IsUnauthorized()) { errorMessage = scopeRequired.IsEmpty() ? "Authentication is required. See https://www.hipchat.com/docs/apiv2/auth" : "Authentication required, this call requires scope '{0}'. See https://www.hipchat.com/docs/apiv2/auth" .Fmt(scopeRequired); return new HipchatWebException(errorMessage,exception); } return exception; }