private static void respond503Unavailable(HttpListenerContext ctx) { try { ctx.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable; using (StreamWriter w = new StreamWriter(ctx.Response.OutputStream)) { Cloud.CloudResponse json = new Cloud.CloudResponse( ctx.Response.StatusCode, DateTime.UtcNow, (int)StationApiError.ServerOffline, "Server offline"); w.Write(json.ToFastJSON()); } } catch (Exception e) { logger.Warn("Unable to respond 503 Service Unavailable", e); } }
private static void respond401Unauthorized(HttpListenerContext ctx) { try { ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; using (StreamWriter w = new StreamWriter(ctx.Response.OutputStream)) { Cloud.CloudResponse json = new Cloud.CloudResponse( ctx.Response.StatusCode, DateTime.UtcNow, (int)StationApiError.AlreadyHasStaion, "Driver already registered another station" ); w.Write(json.ToFastJSON()); } } catch (Exception e) { logger.Warn("Unable to respond 401 Unauthorized", e); } }
public WammerStationException(Cloud.CloudResponse errorResp) : base(errorResp.api_ret_message) { this.wammerError = errorResp.api_ret_code; this.ErrorResponse = errorResp; }
public void Do(object state) { HttpData ctx = (HttpData)state; try { IHttpHandler clonedHandler = (IHttpHandler)this.handler.Clone(); clonedHandler.SetBeginTimestamp(ctx.BeginTime); clonedHandler.HandleRequest(ctx.Context.Request, ctx.Context.Response); } catch (Cloud.WammerCloudException e) { // cannot connect to Waveface cloud if (e.HttpError != WebExceptionStatus.ProtocolError) { HttpHelper.RespondFailure(ctx.Context.Response, new WammerStationException(e.InnerException.Message, (int)StationApiError.ConnectToCloudError), (int)HttpStatusCode.BadRequest); logger.Warn("Connection to cloud error", e); return; } // if cloud returns bad request error, bypass it to client WebException webex = (WebException)e.InnerException; if (webex != null) { HttpWebResponse webres = (HttpWebResponse)webex.Response; if (webres != null) { if (webres.StatusCode == HttpStatusCode.BadRequest) { Cloud.CloudResponse cloudres = fastJSON.JSON.Instance.ToObject <Cloud.CloudResponse>(e.response); HttpHelper.RespondFailure(ctx.Context.Response, cloudres); logger.Warn("Connection to cloud error", e); return; } HttpHelper.RespondFailure(ctx.Context.Response, new WammerStationException(e.InnerException.Message, e.WammerError), (int)webres.StatusCode); logger.Warn("Connecting to cloud error", e); return; } } HttpHelper.RespondFailure(ctx.Context.Response, new WammerStationException(e.Message, e.WammerError), (int)HttpStatusCode.BadRequest); logger.Warn("Connecting to cloud error", e); } catch (ServiceUnavailableException e) { HttpHelper.RespondFailure(ctx.Context.Response, e, (int)HttpStatusCode.ServiceUnavailable); logger.Warn("Service unavailable", e); } catch (WammerStationException e) { HttpHelper.RespondFailure(ctx.Context.Response, e, (int)HttpStatusCode.BadRequest); logger.Warn("Http handler error", e); } catch (FormatException e) { HttpHelper.RespondFailure(ctx.Context.Response, e, (int)HttpStatusCode.BadRequest); logger.Warn("Request format is incorrect", e); } catch (WebException webex) { Wammer.Cloud.WammerCloudException ex = new Cloud.WammerCloudException("Request to cloud failed", webex); HttpHelper.RespondFailure(ctx.Context.Response, webex, (int)HttpStatusCode.InternalServerError); logger.Warn("Connecting to cloud error", ex); } catch (Exception e) { HttpHelper.RespondFailure(ctx.Context.Response, e, (int)HttpStatusCode.InternalServerError); logger.Warn("Internal server error", e); } }
public ServiceUnavailableException(Cloud.CloudResponse errorResp) : base(errorResp.api_ret_message) { this.wammerError = errorResp.api_ret_code; this.ErrorResponse = errorResp; }
private static void ExtractApiRetMsg(Cloud.WammerCloudException e) { if (e.HttpError != WebExceptionStatus.ProtocolError) { if (e.InnerException != null) { throw new ConnectToCloudException(e.InnerException.Message); } else { throw new ConnectToCloudException(e.Message); } } WebException webex = (WebException)e.InnerException; if (webex != null) { HttpWebResponse webres = (HttpWebResponse)webex.Response; if (webres != null) { if (webres.StatusCode == HttpStatusCode.BadRequest) { const int ERR_USER_HAS_ANOTHER_STATION = 0x4000 + 3; const int ERR_USER_DOES_NOT_EXIST = 0x4000 + 4; const int ERR_BAD_NAME_PASSWORD = 0x1000 + 1; string resText = e.response; Cloud.CloudResponse r = fastJSON.JSON.Instance.ToObject <Cloud.CloudResponse>(resText); switch (e.WammerError) { case ERR_BAD_NAME_PASSWORD: throw new AuthenticationException(r.api_ret_message); case ERR_USER_HAS_ANOTHER_STATION: throw new UserAlreadyHasStationException(r.api_ret_message); case ERR_USER_DOES_NOT_EXIST: throw new UserDoesNotExistException(r.api_ret_message); case (int)StationApiError.InvalidDriver: throw new InvalidDriverException(r.api_ret_message); case (int)StationApiError.ConnectToCloudError: throw new ConnectToCloudException(r.api_ret_message); default: throw new Exception(r.api_ret_message); } } else if (webres.StatusCode == HttpStatusCode.Unauthorized) { throw new AuthenticationException("Authentication failure"); } else if (webres.StatusCode == HttpStatusCode.ServiceUnavailable) { throw new UserAlreadyHasStationException("Driver already registered another station"); } } else { throw new Exception(webex.Message); } } }