<NME> GenieExceptionMapper.java
<BEF> /*
 *
 *  Copyright 2015 Netflix, Inc.
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 *
 */
package com.netflix.genie.web.apis.rest.v3.controllers;

import com.google.common.collect.Sets;
import com.netflix.genie.common.exceptions.GenieException;
import com.netflix.genie.common.exceptions.GeniePreconditionException;
import com.netflix.genie.common.exceptions.GenieUserLimitExceededException;
import com.netflix.genie.common.internal.exceptions.checked.GenieCheckedException;
import com.netflix.genie.common.internal.exceptions.checked.GenieJobResolutionException;
import com.netflix.genie.common.internal.exceptions.unchecked.GenieApplicationNotFoundException;
import com.netflix.genie.common.internal.exceptions.unchecked.GenieClusterNotFoundException;
import com.netflix.genie.common.internal.exceptions.unchecked.GenieCommandNotFoundException;
import com.netflix.genie.common.internal.exceptions.unchecked.GenieIdAlreadyExistsException;
import com.netflix.genie.common.internal.exceptions.unchecked.GenieJobNotFoundException;
import com.netflix.genie.common.internal.exceptions.unchecked.GenieJobSpecificationNotFoundException;
import com.netflix.genie.common.internal.exceptions.unchecked.GenieRuntimeException;
import com.netflix.genie.web.exceptions.checked.AttachmentTooLargeException;
import com.netflix.genie.web.exceptions.checked.IdAlreadyExistsException;
import com.netflix.genie.web.exceptions.checked.JobNotFoundException;
import com.netflix.genie.web.exceptions.checked.NotFoundException;
import com.netflix.genie.web.exceptions.checked.PreconditionFailedException;
import com.netflix.genie.web.util.MetricsConstants;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.validation.ConstraintViolationException;
import java.util.Set;

/**
 * Exception mapper for Genie Exceptions.
 *
 * @author tgianos
 * @since 3.0.0
 */
@Slf4j
@ControllerAdvice
public class GenieExceptionMapper {

    // TODO: Not changing this while changing controller package due to need to keep dashboards in sync but we should
    //       rename it going forward - TJG 7/17/19
    static final String CONTROLLER_EXCEPTION_COUNTER_NAME = "genie.web.controllers.exception";
    private static final String USER_NAME_TAG_KEY = "user";
    private static final String LIMIT_TAG_KEY = "limit";

    private final MeterRegistry registry;

    /**
     * Constructor.
     *
     * @param registry The metrics registry
     */
    @Autowired
    public GenieExceptionMapper(final MeterRegistry registry) {
        this.registry = registry;
    }

    /**
     * Handle Genie Exceptions.
     */
    @ExceptionHandler(GenieException.class)
    public ResponseEntity<Object> handleGenieException(final GenieException e) {
        this.countException(e);
        log.error(e.getLocalizedMessage(), e);
        HttpStatus status = HttpStatus.resolve(e.getErrorCode());
        if (status == null) {
            status = HttpStatus.INTERNAL_SERVER_ERROR;
        HttpStatus status = HttpStatus.resolve(e.getErrorCode());
        if (status == null) {
            status = HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return new ResponseEntity<>(e, status);
    }

    /**
     * Handle Genie runtime exceptions.
     */
    @ExceptionHandler(GenieRuntimeException.class)
    public ResponseEntity<Object> handleGenieRuntimeException(final GenieRuntimeException e) {
        this.countException(e);
        if (
            e instanceof GenieApplicationNotFoundException
                || e instanceof GenieCommandNotFoundException
        if (
            e instanceof GenieApplicationNotFoundException
                || e instanceof GenieCommandNotFoundException
                || e instanceof GenieClusterNotFoundException
                || e instanceof GenieJobNotFoundException
                || e instanceof GenieJobSpecificationNotFoundException
        ) {
            return new ResponseEntity<>(e, HttpStatus.NOT_FOUND);
        } else if (e instanceof GenieIdAlreadyExistsException) {
            return new ResponseEntity<>(e, HttpStatus.CONFLICT);
        } else {
            return new ResponseEntity<>(e, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * Handle {@link GenieCheckedException} instances.
     */
    @ExceptionHandler(GenieCheckedException.class)
    public ResponseEntity<Object> handleGenieCheckedException(final GenieCheckedException e) {
        this.countException(e);
        if (e instanceof GenieJobResolutionException) {
            // Mapped to Precondition failed to maintain existing contract with V3
            return new ResponseEntity<>(e.getMessage(), HttpStatus.PRECONDITION_FAILED);
        if (e instanceof GenieJobResolutionException) {
            // Mapped to Precondition failed to maintain existing contract with V3
            return new ResponseEntity<>(e, HttpStatus.PRECONDITION_FAILED);
        } else if (e instanceof IdAlreadyExistsException) {
            return new ResponseEntity<>(e, HttpStatus.CONFLICT);
        } else if (e instanceof JobNotFoundException | e instanceof NotFoundException) {
            return new ResponseEntity<>(e, HttpStatus.NOT_FOUND);
        } else if (e instanceof PreconditionFailedException) {
            return new ResponseEntity<>(e, HttpStatus.BAD_REQUEST);
        } else if (e instanceof AttachmentTooLargeException) {
            return new ResponseEntity<>(e, HttpStatus.PAYLOAD_TOO_LARGE);
        } else {
            return new ResponseEntity<>(e, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * Handle constraint violation exceptions.
     *
     * @param cve The exception to handle
     * @return A {@link ResponseEntity} instance
                builder.append(cv.getMessage());
            }
        }
        this.countException(cve);
        log.error(cve.getLocalizedMessage(), cve);
        return new ResponseEntity<>(builder.toString(), HttpStatus.PRECONDITION_FAILED);
    }

            new GeniePreconditionException(cve.getMessage(), cve),
            HttpStatus.PRECONDITION_FAILED
        );
    }

     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<Object> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
        this.countException(e);
        final String errorMessage = e.getMessage();
        log.error(errorMessage, e);
        return new ResponseEntity<>(errorMessage, HttpStatus.PRECONDITION_FAILED);
    }

    private void countException(final Exception e) {
        final Set<Tag> tags = Sets.newHashSet(
            Tags.of(MetricsConstants.TagKeys.EXCEPTION_CLASS, e.getClass().getCanonicalName())
        );
            new GeniePreconditionException(e.getMessage(), e),
            HttpStatus.PRECONDITION_FAILED
        );
    }

        }

        this.registry.counter(CONTROLLER_EXCEPTION_COUNTER_NAME, tags).increment();
    }
}
            tags.add(Tag.of(LIMIT_TAG_KEY, userLimitExceededException.getExceededLimitName()));
        }

        this.registry.counter(CONTROLLER_EXCEPTION_COUNTER_NAME, tags).increment();

        log.error("{}: {}", e.getClass().getSimpleName(), e.getLocalizedMessage());
        log.debug("{}: {}", e.getClass().getCanonicalName(), e.getMessage(), e);
    }
}

<MSG> Adjust verbosity of GenieExceptionMapper

 - Consistently print exception class and message for all exception
 - Print the full stack trace in DEBUG level

Rationale for this change:
 - Full stack traces in the logs are voluminous and hard to filter out (multi-line)
 - Most exception caught and printed are part of regular operations (e.g. NotFound / Duplicate Id used by workflow to determine if a step executed)
 - Most log messages are self-contained and clear, and the stack trace does not add value (e.g.: File not found, Constraint violation, Illegal arguments)
 - Logging the exception with enough info to debug should be responsibility of individual components, and not the job of the mapper

<DFF> @@ -83,8 +83,7 @@ public class GenieExceptionMapper {
      */
     @ExceptionHandler(GenieException.class)
     public ResponseEntity<Object> handleGenieException(final GenieException e) {
-        this.countException(e);
-        log.error(e.getLocalizedMessage(), e);
+        this.countExceptionAndLog(e);
         HttpStatus status = HttpStatus.resolve(e.getErrorCode());
         if (status == null) {
             status = HttpStatus.INTERNAL_SERVER_ERROR;
@@ -100,7 +99,7 @@ public class GenieExceptionMapper {
      */
     @ExceptionHandler(GenieRuntimeException.class)
     public ResponseEntity<Object> handleGenieRuntimeException(final GenieRuntimeException e) {
-        this.countException(e);
+        this.countExceptionAndLog(e);
         if (
             e instanceof GenieApplicationNotFoundException
                 || e instanceof GenieCommandNotFoundException
@@ -124,7 +123,7 @@ public class GenieExceptionMapper {
      */
     @ExceptionHandler(GenieCheckedException.class)
     public ResponseEntity<Object> handleGenieCheckedException(final GenieCheckedException e) {
-        this.countException(e);
+        this.countExceptionAndLog(e);
         if (e instanceof GenieJobResolutionException) {
             // Mapped to Precondition failed to maintain existing contract with V3
             return new ResponseEntity<>(e.getMessage(), HttpStatus.PRECONDITION_FAILED);
@@ -152,8 +151,7 @@ public class GenieExceptionMapper {
                 builder.append(cv.getMessage());
             }
         }
-        this.countException(cve);
-        log.error(cve.getLocalizedMessage(), cve);
+        this.countExceptionAndLog(cve);
         return new ResponseEntity<>(builder.toString(), HttpStatus.PRECONDITION_FAILED);
     }
 
@@ -165,13 +163,12 @@ public class GenieExceptionMapper {
      */
     @ExceptionHandler(MethodArgumentNotValidException.class)
     public ResponseEntity<Object> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
-        this.countException(e);
-        final String errorMessage = e.getMessage();
-        log.error(errorMessage, e);
+        this.countExceptionAndLog(e);
+        final String errorMessage = e.getLocalizedMessage();
         return new ResponseEntity<>(errorMessage, HttpStatus.PRECONDITION_FAILED);
     }
 
-    private void countException(final Exception e) {
+    private void countExceptionAndLog(final Exception e) {
         final Set<Tag> tags = Sets.newHashSet(
             Tags.of(MetricsConstants.TagKeys.EXCEPTION_CLASS, e.getClass().getCanonicalName())
         );
@@ -183,5 +180,8 @@ public class GenieExceptionMapper {
         }
 
         this.registry.counter(CONTROLLER_EXCEPTION_COUNTER_NAME, tags).increment();
+
+        log.error("{}: {}", e.getClass().getSimpleName(), e.getLocalizedMessage());
+        log.debug("{}: {}", e.getClass().getCanonicalName(), e.getMessage(), e);
     }
 }

Adjust verbosity of GenieExceptionMapper