<commit_before>package net.trajano.ms.gateway.handlers;

import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE;

import java.net.ConnectException;
import java.net.UnknownHostException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import io.vertx.core.http.HttpHeaders;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import net.trajano.ms.gateway.internal.Errors;
import net.trajano.ms.gateway.internal.MediaTypes;

/**
 * This provides the error handling for those requests that have not been
 * handled correctly in the upstream routes.
 *
 * @author <NAME>
 */
@Component
@Order(SelfRegisteringRoutingContextHandler.CORE_GLOBAL)
public class GlobalFailureHandler extends SelfRegisteringRoutingContextHandler {

    private static final String GATEWAY_ERROR = "Gateway Error";

    private static final String GATEWAY_TIMEOUT = "Gateway Timeout";

    private static final String INTERNAL_SERVER_ERROR = "Internal Server Error";

    private static final Logger LOG = LoggerFactory.getLogger(GlobalFailureHandler.class);

    @Override
    public void handle(final RoutingContext context) {

        LOG.error("Unhandled server exception statusCode={} responseEnded={}", context.statusCode(), context.response().ended(), context.failure());
        if (!context.response().ended()) {
            if (context.failure() instanceof ConnectException) {
                context.response().setStatusCode(504)
                    .setStatusMessage(GATEWAY_TIMEOUT)
                    .putHeader(CONTENT_TYPE, MediaTypes.APPLICATION_JSON)
                    .end(Errors.serverError(GATEWAY_TIMEOUT).toBuffer());
            } else if (context.failure() instanceof UnknownHostException) {
                context.response().setStatusCode(503)
                    .setStatusMessage(GATEWAY_ERROR)
                    .putHeader(CONTENT_TYPE, MediaTypes.APPLICATION_JSON)
                    .end(Errors.serverError(GATEWAY_ERROR).toBuffer());
            } else {
                context.response().setStatusCode(context.statusCode())
                    .setStatusMessage(INTERNAL_SERVER_ERROR)
                    .putHeader(HttpHeaders.CONTENT_TYPE, MediaTypes.APPLICATION_JSON)
                    .end(Errors.serverError(INTERNAL_SERVER_ERROR).toBuffer());
            }<commit_msg>Added extra data to the logger<commit_after>package net.trajano.ms.gateway.handlers;

import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE;

import java.net.ConnectException;
import java.net.UnknownHostException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import io.vertx.core.http.HttpHeaders;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import net.trajano.ms.gateway.internal.Errors;
import net.trajano.ms.gateway.internal.MediaTypes;

/**
 * This provides the error handling for those requests that have not been
 * handled correctly in the upstream routes.
 *
 * @author <NAME>
 */
@Component
@Order(SelfRegisteringRoutingContextHandler.CORE_GLOBAL)
public class GlobalFailureHandler extends SelfRegisteringRoutingContextHandler {

    private static final String GATEWAY_ERROR = "Gateway Error";

    private static final String GATEWAY_TIMEOUT = "Gateway Timeout";

    private static final String INTERNAL_SERVER_ERROR = "Internal Server Error";

    private static final Logger LOG = LoggerFactory.getLogger(GlobalFailureHandler.class);

    @Override
    public void handle(final RoutingContext context) {

        if (LOG.isErrorEnabled()) {
            LOG.error("Unhandled server exception statusCode={} responseEnded={} uri={}", context.statusCode(), context.response().ended(), context.request().uri(), context.failure());
        }
        if (!context.response().ended()) {
            if (context.failure() instanceof ConnectException) {
                context.response().setStatusCode(504)
                    .setStatusMessage(GATEWAY_TIMEOUT)
                    .putHeader(CONTENT_TYPE, MediaTypes.APPLICATION_JSON)
                    .end(Errors.serverError(GATEWAY_TIMEOUT).toBuffer());
            } else if (context.failure() instanceof UnknownHostException) {
                context.response().setStatusCode(503)
                    .setStatusMessage(GATEWAY_ERROR)
                    .putHeader(CONTENT_TYPE, MediaTypes.APPLICATION_JSON)
                    .end(Errors.serverError(GATEWAY_ERROR).toBuffer());
            } else {
                context.response().setStatusCode(context.statusCode())
                    .setStatusMessage(INTERNAL_SERVER_ERROR)
                    .putHeader(HttpHeaders.CONTENT_TYPE, MediaTypes.APPLICATION_JSON)
                    .end(Errors.serverError(INTERNAL_SERVER_ERROR).toBuffer());
            }
{'fraction_non_alphanumeric': 0.09730932632004856, 'fraction_numerical': 0.0032369006676107627, 'mean_word_length': 3.2805194805194806, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3526841', 'n_tokens_mistral': 1331, 'n_tokens_neox': 1309, 'n_words': 255}