Java Code Examples for com.linecorp.armeria.common.HttpStatus#codeClass()

The following examples show how to use com.linecorp.armeria.common.HttpStatus#codeClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArmeriaCentralDogma.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
private static <T> T handleErrorResponse(AggregatedHttpResponse res) {
    final HttpStatus status = res.status();
    if (status.codeClass() != HttpStatusClass.SUCCESS) {
        final JsonNode node = toJson(res, JsonNodeType.OBJECT);
        final JsonNode exceptionNode = node.get("exception");
        final JsonNode messageNode = node.get("message");

        if (exceptionNode != null) {
            final String typeName = exceptionNode.textValue();
            if (typeName != null) {
                final Function<String, CentralDogmaException> exceptionFactory =
                        EXCEPTION_FACTORIES.get(typeName);
                if (exceptionFactory != null) {
                    throw exceptionFactory.apply(messageNode.textValue());
                }
            }
        }
    }

    throw new CentralDogmaException("unexpected response: " + res.headers() + ", " + res.contentUtf8());
}
 
Example 2
Source File: HttpApiUtil.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
private static HttpResponse newResponse0(RequestContext ctx, HttpStatus status,
                                         @Nullable Throwable cause, @Nullable String message) {
    checkArgument(!status.isContentAlwaysEmpty(),
                  "status: %s (expected: a status with non-empty content)", status);

    final ObjectNode node = JsonNodeFactory.instance.objectNode();
    if (cause != null) {
        node.put("exception", cause.getClass().getName());
        if (message == null) {
            message = cause.getMessage();
        }
    }

    final String m = nullToEmpty(message);
    node.put("message", m);

    final LogLevel logLevel;
    switch (status.codeClass()) {
        case SERVER_ERROR:
            if (cause != null) {
                if (!(Exceptions.isStreamCancelling(cause) ||
                      cause instanceof ShuttingDownException)) {
                    logLevel = LogLevel.WARN;
                } else {
                    logLevel = null;
                }
            } else {
                logLevel = LogLevel.WARN;
            }
            break;
        case CLIENT_ERROR:
            logLevel = LogLevel.DEBUG;
            break;
        case UNKNOWN:
            logLevel = LogLevel.WARN;
            break;
        default:
            logLevel = null;
    }

    // TODO(trustin): Use LogLevel.OFF instead of null and logLevel.log()
    //                once we add LogLevel.OFF and LogLevel.log() with more args.
    if (logLevel != null) {
        if (logLevel == LogLevel.WARN) {
            if (cause != null) {
                logger.warn(ERROR_MESSAGE_FORMAT, ctx, status, m, cause);
            } else {
                logger.warn(ERROR_MESSAGE_FORMAT, ctx, status, m);
            }
        } else {
            if (cause != null) {
                logger.debug(ERROR_MESSAGE_FORMAT, ctx, status, m, cause);
            } else {
                logger.debug(ERROR_MESSAGE_FORMAT, ctx, status, m);
            }
        }
    }

    // TODO(hyangtack) Need to introduce a new field such as 'stackTrace' in order to return
    //                 the stack trace of the cause to the trusted client.
    try {
        return HttpResponse.of(status, MediaType.JSON_UTF_8, Jackson.writeValueAsBytes(node));
    } catch (JsonProcessingException e) {
        // should not reach here
        throw new Error(e);
    }
}
 
Example 3
Source File: HttpHealthChecker.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public void onNext(HttpObject obj) {
    if (closeable.isClosing()) {
        subscription.cancel();
        return;
    }

    try {
        if (!(obj instanceof ResponseHeaders)) {
            ReferenceCountUtil.release(obj);
            return;
        }

        final ResponseHeaders headers = (ResponseHeaders) obj;
        updateLongPollingSettings(headers);

        final HttpStatus status = headers.status();
        final HttpStatusClass statusClass = status.codeClass();
        switch (statusClass) {
            case INFORMATIONAL:
                maybeSchedulePingCheck();
                break;
            case SERVER_ERROR:
                receivedExpectedResponse = true;
                break;
            case SUCCESS:
                isHealthy = true;
                receivedExpectedResponse = true;
                break;
            default:
                if (status == HttpStatus.NOT_MODIFIED) {
                    isHealthy = wasHealthy;
                    receivedExpectedResponse = true;
                } else {
                    // Do not use long polling on an unexpected status for safety.
                    maxLongPollingSeconds = 0;

                    if (statusClass == HttpStatusClass.CLIENT_ERROR) {
                        logger.warn("{} Unexpected 4xx health check response: {} A 4xx response " +
                                    "generally indicates a misconfiguration of the client. " +
                                    "Did you happen to forget to configure the {}'s client options?",
                                    reqCtx, headers, HealthCheckedEndpointGroup.class.getSimpleName());
                    } else {
                        logger.warn("{} Unexpected health check response: {}", reqCtx, headers);
                    }
                }
        }
    } finally {
        subscription.request(1);
    }
}