private ErrorException ErrorExceptionFromWebException(WebException e) { var httpWebResponse = e.Response as HttpWebResponse; if (null == httpWebResponse) { // some kind of network error: didn't even make a connection return(new ErrorException(e.Message, e)); } var statusCode = (HttpStatusCode)httpWebResponse.StatusCode; switch (statusCode) { case HttpStatusCode.Unauthorized: case HttpStatusCode.NotFound: case HttpStatusCode.MethodNotAllowed: case HttpStatusCode.UnprocessableEntity: using (var responseReader = new StreamReader(httpWebResponse.GetResponseStream())) { ErrorException errorException = ErrorException.FromResponse(responseReader.ReadToEnd(), e); if (errorException != null) { return(errorException); } return(new ErrorException(String.Format("Unknown error for {0}", statusCode), e)); } case HttpStatusCode.InternalServerError: case HttpStatusCode.NotImplemented: case HttpStatusCode.BadGateway: case HttpStatusCode.ServiceUnavailable: case HttpStatusCode.GatewayTimeout: case HttpStatusCode.HttpVersionNotSupported: case HttpStatusCode.VariantAlsoNegotiates: case HttpStatusCode.InsufficientStorage: case HttpStatusCode.LoopDetected: case HttpStatusCode.BandwidthLimitExceeded: case HttpStatusCode.NotExtended: case HttpStatusCode.NetworkAuthenticationRequired: case HttpStatusCode.NetworkReadTimeoutError: case HttpStatusCode.NetworkConnectTimeoutError: return(new ErrorException("Something went wrong on our end, please try again", e)); default: return(new ErrorException(String.Format("Unhandled status code {0}", statusCode), e)); } }
/// <summary>Parse a WAMP message from the server.</summary> private Response Parse(string msg) { const string msgTypePattern = @"^\[\s*(\d+)"; var match = System.Text.RegularExpressions.Regex.Match(msg, msgTypePattern, System.Text.RegularExpressions.RegexOptions.Singleline); if (match.Groups.Count != 2) { throw new ErrorException("Error while parsing response from server."); } Messages messageId = (Messages)int.Parse(match.Groups[1].Value); switch (messageId) { case Messages.WELCOME: return(ParseWelcome(msg)); case Messages.GOODBYE: return(ParseGoodbye(msg)); case Messages.SUBSCRIBED: return(ParseSubscribed(msg)); case Messages.UNSUBSCRIBED: return(ParseUnsubscribed(msg)); case Messages.EVENT: return(ParseEvent(msg)); case Messages.RESULT: return(ParseResult(msg)); case Messages.ERROR: throw ErrorException.FromResponse(msg); default: throw new ErrorException("Unexpected result from server."); } }