Exemplo n.º 1
0
        private void writeResponse(AnyResponse data)
        {
            var userError       = data.Error is ApplicationException;
            var responseIsData  = data.Json != null;
            var responseIsImage = data.Image != null;
            var trackingId      = _request.TrackingId;

            var response = _request.Http.Response;

            if (data.Error != null)
            {
                _log.error(_request.TrackingId, "anyhandler", data.Error);
            }

            if (userError)
            {
                response.ContentType = "application/json";
                response.StatusCode  = 400;
                var d = new {
                    Error = $"{data.Error.Message} (trackingId='{trackingId}')"
                };
                response.Write(d.ToJson());
                return;
            }

            if (responseIsData)
            {
                response.ContentType = "application/json";
                response.Write(data.Json);
                return;
            }

            if (responseIsImage)
            {
                response.ContentType = "image/png";
                response.BinaryWrite(data.Image);
                return;
            }

            // internal error and fallback

            response.ContentType = "application/json";
            response.StatusCode  = 500;
            var err = new {
                Error = $"Oops... Something went wrong (trackingId='{trackingId}')"
            };

            response.Write(err.ToJson());
        }
Exemplo n.º 2
0
 void IAnyHandler.WriteResponse(AnyResponse data)
 {
     writeResponse(data);
 }