Search in sources :

Example 1 with H2OErrorV3

use of water.api.schemas3.H2OErrorV3 in project h2o-3 by h2oai.

the class JettyHTTPD method sendErrorResponse.

public static void sendErrorResponse(HttpServletResponse response, Exception e, String uri) {
    if (e instanceof H2OFailException) {
        H2OFailException ee = (H2OFailException) e;
        H2OError error = ee.toH2OError(uri);
        Log.fatal("Caught exception (fatal to the cluster): " + error.toString());
        throw (H2O.fail(error.toString()));
    } else if (e instanceof H2OAbstractRuntimeException) {
        H2OAbstractRuntimeException ee = (H2OAbstractRuntimeException) e;
        H2OError error = ee.toH2OError(uri);
        Log.warn("Caught exception: " + error.toString());
        setResponseStatus(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        // Note: don't use Schema.schema(version, error) because we have to work at bootstrap:
        try {
            @SuppressWarnings("unchecked") String s = new H2OErrorV3().fillFromImpl(error).toJsonString();
            response.getWriter().write(s);
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
    } else {
        // make sure that no Exception is ever thrown out from the request
        H2OError error = new H2OError(e, uri);
        // some special cases for which we return 400 because it's likely a problem with the client request:
        if (e instanceof IllegalArgumentException)
            error._http_status = HttpResponseStatus.BAD_REQUEST.getCode();
        else if (e instanceof FileNotFoundException)
            error._http_status = HttpResponseStatus.BAD_REQUEST.getCode();
        else if (e instanceof MalformedURLException)
            error._http_status = HttpResponseStatus.BAD_REQUEST.getCode();
        setResponseStatus(response, error._http_status);
        Log.warn("Caught exception: " + error.toString());
        // Note: don't use Schema.schema(version, error) because we have to work at bootstrap:
        try {
            @SuppressWarnings("unchecked") String s = new H2OErrorV3().fillFromImpl(error).toJsonString();
            response.getWriter().write(s);
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) H2OErrorV3(water.api.schemas3.H2OErrorV3) H2OFailException(water.exceptions.H2OFailException) H2OAbstractRuntimeException(water.exceptions.H2OAbstractRuntimeException) ServletException(javax.servlet.ServletException) H2OFailException(water.exceptions.H2OFailException) H2OAbstractRuntimeException(water.exceptions.H2OAbstractRuntimeException) MalformedURLException(java.net.MalformedURLException)

Example 2 with H2OErrorV3

use of water.api.schemas3.H2OErrorV3 in project h2o-3 by h2oai.

the class RequestServer method serveSchema.

private static NanoResponse serveSchema(Schema s, RequestType type) {
    // Convert Schema to desired output flavor
    String http_response_header = H2OError.httpStatusHeader(HttpResponseStatus.OK.getCode());
    // If we're given an http response code use it.
    if (s instanceof SpecifiesHttpResponseCode) {
        http_response_header = H2OError.httpStatusHeader(((SpecifiesHttpResponseCode) s).httpStatus());
    }
    // If we've gotten an error always return the error as JSON
    if (s instanceof SpecifiesHttpResponseCode && HttpResponseStatus.OK.getCode() != ((SpecifiesHttpResponseCode) s).httpStatus()) {
        type = RequestType.json;
    }
    if (s instanceof H2OErrorV3) {
        return new NanoResponse(http_response_header, MIME_JSON, s.toJsonString());
    }
    if (s instanceof StreamingSchema) {
        StreamingSchema ss = (StreamingSchema) s;
        NanoResponse r = new NanoStreamResponse(http_response_header, MIME_DEFAULT_BINARY, ss.getStreamWriter());
        // Needed to make file name match class name
        r.addHeader("Content-Disposition", "attachment; filename=\"" + ss.getFilename() + "\"");
        return r;
    }
    // TODO: remove this entire switch
    switch(type) {
        // return JSON for html requests
        case html:
        case json:
            return new NanoResponse(http_response_header, MIME_JSON, s.toJsonString());
        case xml:
            throw H2O.unimpl("Unknown type: " + type.toString());
        case java:
            if (s instanceof AssemblyV99) {
                // TODO: fix the AssemblyV99 response handler so that it produces the appropriate StreamingSchema
                Assembly ass = DKV.getGet(((AssemblyV99) s).assembly_id);
                NanoResponse r = new NanoResponse(http_response_header, MIME_DEFAULT_BINARY, ass.toJava(((AssemblyV99) s).pojo_name));
                r.addHeader("Content-Disposition", "attachment; filename=\"" + JCodeGen.toJavaId(((AssemblyV99) s).pojo_name) + ".java\"");
                return r;
            } else {
                throw new H2OIllegalArgumentException("Cannot generate java for type: " + s.getClass().getSimpleName());
            }
        default:
            throw H2O.unimpl("Unknown type to serveSchema(): " + type);
    }
}
Also used : AssemblyV99(water.api.schemas99.AssemblyV99) H2OErrorV3(water.api.schemas3.H2OErrorV3) Assembly(water.rapids.Assembly)

Aggregations

H2OErrorV3 (water.api.schemas3.H2OErrorV3)2 MalformedURLException (java.net.MalformedURLException)1 ServletException (javax.servlet.ServletException)1 AssemblyV99 (water.api.schemas99.AssemblyV99)1 H2OAbstractRuntimeException (water.exceptions.H2OAbstractRuntimeException)1 H2OFailException (water.exceptions.H2OFailException)1 Assembly (water.rapids.Assembly)1