Skip to content

Commit

Permalink
Included some examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels Dommerholt committed Dec 27, 2016
1 parent e1cc3dc commit 51113f3
Show file tree
Hide file tree
Showing 25 changed files with 261 additions and 28 deletions.
15 changes: 15 additions & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>base-error-handler</artifactId>
<groupId>com.nibado.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>examples</artifactId>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nibado.example.errorhandlers.example1;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "User not found")
public class UserNotFoundException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.nibado.example.errorhandlers.example2;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
@Slf4j
public class ExceptionHandlers {
@ExceptionHandler(UserNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public ErrorResponse handleUserNotFoundException(final UserNotFoundException ex) {
log.error("User not found thrown", ex);
return new ErrorResponse("USER_NOT_FOUND", "The user was not found");
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Throwable.class)
@ResponseBody
public ErrorResponse handleThrowable(final Throwable ex) {
log.error("Unexpected error", ex);
return new ErrorResponse("INTERNAL_SERVER_ERROR", "An unexpected internal server error occured");
}

@Data
public static class ErrorResponse {
private final String code;
private final String message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.nibado.example.errorhandlers.example2;

public class UserNotFoundException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.nibado.example.errorhandlers.example3;

import lombok.Data;
import org.slf4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

public abstract class BaseExceptionHandler {
private final Logger log;

protected BaseExceptionHandler(final Logger log) {
this.log = log;
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Throwable.class)
@ResponseBody
public ExceptionHandlers.ErrorResponse handleThrowable(final Throwable ex) {
log.error("Unexpected error", ex);
return new ErrorResponse("INTERNAL_SERVER_ERROR", "An unexpected internal server error occured");
}

@Data
public static class ErrorResponse {
private final String code;
private final String message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.nibado.example.errorhandlers.example3;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
@Slf4j
public class ExceptionHandlers extends BaseExceptionHandler {
public ExceptionHandlers() {
super(log);
}

@ExceptionHandler(UserNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public ErrorResponse handleUserNotFoundException(final UserNotFoundException ex) {
log.error("User not found thrown", ex);
return new ErrorResponse("USER_NOT_FOUND", "The user was not found");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.nibado.example.errorhandlers.example3;

public class UserNotFoundException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.nibado.example.errorhandlers.example4;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

import static org.springframework.http.HttpStatus.*;

public abstract class BaseExceptionHandler {
private static final ExceptionMapping DEFAULT_ERROR = new ExceptionMapping(
"SERVER_ERROR",
"Internal server error",
INTERNAL_SERVER_ERROR);

private final Logger log;
private final Map<Class, ExceptionMapping> exceptionMappings = new HashMap<>();

public BaseExceptionHandler(final Logger log) {
this.log = log;

registerMapping(
MissingServletRequestParameterException.class,
"MISSING_PARAMETER",
"Missing request parameter",
BAD_REQUEST);
registerMapping(
MethodArgumentTypeMismatchException.class,
"ARGUMENT_TYPE_MISMATCH",
"Argument type mismatch",
BAD_REQUEST);
registerMapping(
HttpRequestMethodNotSupportedException.class,
"METHOD_NOT_SUPPORTED",
"HTTP method not supported",
METHOD_NOT_ALLOWED);
registerMapping(
ServletRequestBindingException.class,
"MISSING_HEADER",
"Missing header in request",
BAD_REQUEST);
}

@ExceptionHandler(Throwable.class)
@ResponseBody
public ErrorResponse handleThrowable(final Throwable ex, final HttpServletResponse response) {
ExceptionMapping mapping = exceptionMappings.getOrDefault(ex.getClass(), DEFAULT_ERROR);

response.setStatus(mapping.status.value());

log.error("{} ({}): {}", mapping.message, mapping.code, ex.getMessage(), ex);

return new ErrorResponse(mapping.code, mapping.message);
}

protected void registerMapping(
final Class<?> clazz,
final String code,
final String message,
final HttpStatus status) {
exceptionMappings.put(clazz, new ExceptionMapping(code, message, status));
}

@Data
public static class ErrorResponse {
private final String code;
private final String message;
}

@AllArgsConstructor
private static class ExceptionMapping {
private final String message;
private final String code;
private final HttpStatus status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.nibado.example.errorhandlers.example4;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;

@ControllerAdvice
@Slf4j
public class ExceptionHandlers extends BaseExceptionHandler {
public ExceptionHandlers() {
super(log);
registerMapping(UserNotFoundException.class, "USER_NOT_FOUND", "User not found", HttpStatus.NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.nibado.example.errorhandlers.example4;

public class UserNotFoundException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nibado.example.baseerrorhandler.lib;
package com.nibado.example.errorhandlers.lib;

import org.slf4j.Logger;
import org.springframework.http.HttpStatus;
Expand All @@ -15,7 +15,7 @@

import static org.springframework.http.HttpStatus.*;

public abstract class BaseErrorHandler {
public abstract class BaseExceptionHandler {
private static final ExceptionMapping DEFAULT_ERROR = new ExceptionMapping(
"SERVER_ERROR",
"Internal server error",
Expand All @@ -24,7 +24,7 @@ public abstract class BaseErrorHandler {
private final Logger log;
private final Map<Class, ExceptionMapping> exceptionMappings = new HashMap<>();

public BaseErrorHandler(final Logger log) {
public BaseExceptionHandler(final Logger log) {
this.log = log;

registerMapping(MissingServletRequestParameterException.class, "MISSING_PARAMETER", "Missing request parameter", BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nibado.example.baseerrorhandler.lib;
package com.nibado.example.errorhandlers.lib;

import lombok.Data;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nibado.example.baseerrorhandler.service;
package com.nibado.example.errorhandlers.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nibado.example.baseerrorhandler.service.configuration;
package com.nibado.example.errorhandlers.service.configuration;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.nibado.example.baseerrorhandler.service.controller;
package com.nibado.example.errorhandlers.service.controller;

import com.nibado.example.baseerrorhandler.lib.BaseErrorHandler;
import com.nibado.example.baseerrorhandler.service.controller.exception.NotImplementedException;
import com.nibado.example.baseerrorhandler.service.controller.exception.UserNotFoundException;
import com.nibado.example.baseerrorhandler.service.service.exception.InvalidSearchParamException;
import com.nibado.example.errorhandlers.lib.BaseExceptionHandler;
import com.nibado.example.errorhandlers.service.controller.exception.NotImplementedException;
import com.nibado.example.errorhandlers.service.controller.exception.UserNotFoundException;
import com.nibado.example.errorhandlers.service.service.exception.InvalidSearchParamException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;

@ControllerAdvice
@Slf4j
public class ErrorHandler extends BaseErrorHandler {
public class ErrorHandler extends BaseExceptionHandler {
public ErrorHandler() {
super(log);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.nibado.example.baseerrorhandler.service.controller;
package com.nibado.example.errorhandlers.service.controller;

import com.nibado.example.baseerrorhandler.service.controller.exception.NotImplementedException;
import com.nibado.example.baseerrorhandler.service.service.domain.User;
import com.nibado.example.baseerrorhandler.service.service.UserService;
import com.nibado.example.errorhandlers.service.controller.exception.NotImplementedException;
import com.nibado.example.errorhandlers.service.service.domain.User;
import com.nibado.example.errorhandlers.service.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
Expand All @@ -13,7 +13,7 @@
import java.util.UUID;
import java.util.concurrent.Callable;

import static com.nibado.example.baseerrorhandler.service.controller.exception.UserNotFoundException.supplier;
import static com.nibado.example.errorhandlers.service.controller.exception.UserNotFoundException.supplier;

@RestController
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.nibado.example.errorhandlers.service.controller.exception;

public class NotImplementedException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nibado.example.baseerrorhandler.service.controller.exception;
package com.nibado.example.errorhandlers.service.controller.exception;

import java.util.UUID;
import java.util.function.Supplier;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nibado.example.errorhandlers.service.example;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "User not found")
public class UserNotFoundException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.nibado.example.baseerrorhandler.service.service;
package com.nibado.example.errorhandlers.service.service;

import com.nibado.example.baseerrorhandler.service.service.exception.InvalidSearchParamException;
import com.nibado.example.baseerrorhandler.service.service.domain.User;
import com.nibado.example.errorhandlers.service.service.exception.InvalidSearchParamException;
import com.nibado.example.errorhandlers.service.service.domain.User;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nibado.example.baseerrorhandler.service.service.domain;
package com.nibado.example.errorhandlers.service.service.domain;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nibado.example.baseerrorhandler.service.service.exception;
package com.nibado.example.errorhandlers.service.service.exception;

import java.util.function.Supplier;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nibado.example.baseerrorhandler.service.controller;
package com.nibado.example.errorhandlers.service.controller;

import org.junit.Before;
import org.junit.Test;
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<modules>
<module>my-lib</module>
<module>my-service</module>
<module>examples</module>
</modules>

<properties>
Expand Down

0 comments on commit 51113f3

Please sign in to comment.