コード例 #1
0
        internal static DefaultFullHttpResponse ExceptionCaught(Exception cause)
        {
            Exception e = cause is Exception ? (Exception)cause : new Exception(cause);

            if (Log.IsTraceEnabled())
            {
                Log.Trace("GOT EXCEPITION", e);
            }
            //Convert exception
            if (e is ParamException)
            {
                ParamException paramexception = (ParamException)e;
                e = new ArgumentException("Invalid value for webhdfs parameter \"" + paramexception
                                          .GetParameterName() + "\": " + e.InnerException.Message, e);
            }
            else
            {
                if (e is ContainerException || e is SecurityException)
                {
                    e = ToCause(e);
                }
                else
                {
                    if (e is RemoteException)
                    {
                        e = ((RemoteException)e).UnwrapRemoteException();
                    }
                }
            }
            //Map response status
            HttpResponseStatus s;

            if (e is SecurityException)
            {
                s = HttpResponseStatus.Forbidden;
            }
            else
            {
                if (e is AuthorizationException)
                {
                    s = HttpResponseStatus.Forbidden;
                }
                else
                {
                    if (e is FileNotFoundException)
                    {
                        s = HttpResponseStatus.NotFound;
                    }
                    else
                    {
                        if (e is IOException)
                        {
                            s = HttpResponseStatus.Forbidden;
                        }
                        else
                        {
                            if (e is NotSupportedException)
                            {
                                s = HttpResponseStatus.BadRequest;
                            }
                            else
                            {
                                if (e is ArgumentException)
                                {
                                    s = HttpResponseStatus.BadRequest;
                                }
                                else
                                {
                                    Log.Warn("INTERNAL_SERVER_ERROR", e);
                                    s = HttpResponseStatus.InternalServerError;
                                }
                            }
                        }
                    }
                }
            }
            byte[] js = Sharpen.Runtime.GetBytesForString(JsonUtil.ToJsonString(e), Charsets.
                                                          Utf8);
            DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.Http11, s,
                                                                       Unpooled.WrappedBuffer(js));

            resp.Headers().Set(HttpHeaders.Names.ContentType, WebHdfsHandler.ApplicationJsonUtf8
                               );
            resp.Headers().Set(HttpHeaders.Names.ContentLength, js.Length);
            return(resp);
        }
コード例 #2
0
        public virtual Response ToResponse(Exception e)
        {
            if (Log.IsTraceEnabled())
            {
                Log.Trace("GOT EXCEPITION", e);
            }
            //clear content type
            response.SetContentType(null);
            //Convert exception
            if (e is ParamException)
            {
                ParamException paramexception = (ParamException)e;
                e = new ArgumentException("Invalid value for webhdfs parameter \"" + paramexception
                                          .GetParameterName() + "\": " + e.InnerException.Message, e);
            }
            if (e is ContainerException)
            {
                e = ToCause(e);
            }
            if (e is RemoteException)
            {
                e = ((RemoteException)e).UnwrapRemoteException();
            }
            if (e is SecurityException)
            {
                e = ToCause(e);
            }
            //Map response status
            Response.Status s;
            if (e is SecurityException)
            {
                s = Response.Status.Forbidden;
            }
            else
            {
                if (e is AuthorizationException)
                {
                    s = Response.Status.Forbidden;
                }
                else
                {
                    if (e is FileNotFoundException)
                    {
                        s = Response.Status.NotFound;
                    }
                    else
                    {
                        if (e is IOException)
                        {
                            s = Response.Status.Forbidden;
                        }
                        else
                        {
                            if (e is NotSupportedException)
                            {
                                s = Response.Status.BadRequest;
                            }
                            else
                            {
                                if (e is ArgumentException)
                                {
                                    s = Response.Status.BadRequest;
                                }
                                else
                                {
                                    Log.Warn("INTERNAL_SERVER_ERROR", e);
                                    s = Response.Status.InternalServerError;
                                }
                            }
                        }
                    }
                }
            }
            string js = JsonUtil.ToJsonString(e);

            return(Response.Status(s).Type(MediaType.ApplicationJson).Entity(js).Build());
        }