Skip to content

Commit

Permalink
Merge pull request #457 from this-Aditya/entity-exceptions
Browse files Browse the repository at this point in the history
Sending Response entities as exceptions.
  • Loading branch information
yatharthranjan authored Apr 16, 2024
2 parents 15e217b + cc664e2 commit 6a185fb
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.radarbase.appserver.exception.entity;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.Instant;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class ErrorDetails {
Instant timestamp;
int status;
String message;
String path;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.radarbase.appserver.exception.entity;

import lombok.Getter;

import java.time.Instant;

@Getter
public class ErrorDetailsWithCause extends ErrorDetails {
String cause;
public ErrorDetailsWithCause(Instant timestamp, int status, String cause, String message, String path) {
super(timestamp, status, message, path);
this.cause = cause;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.radarbase.appserver.exception.handler;

import org.radarbase.appserver.exception.*;
import org.radarbase.appserver.exception.entity.ErrorDetails;
import org.radarbase.appserver.exception.entity.ErrorDetailsWithCause;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.WebRequest;

import java.time.Instant;

@ControllerAdvice
public class ResponseEntityExceptionHandler {

@ExceptionHandler(Exception.class)
public final ResponseEntity<ErrorDetails> handleUnhandledException(Exception ex, WebRequest request) {
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
ErrorDetails error;
if (ex.getCause() == null) {
error = new ErrorDetails(Instant.now(), status.value(), ex.getMessage(), request.getDescription(false));
} else {
error = new ErrorDetailsWithCause(Instant.now(), status.value(), ex.getCause().getMessage(), ex.getMessage(), request.getDescription(false));
}
return new ResponseEntity<>(error, status);
}


@ExceptionHandler(InvalidProjectDetailsException.class)
public final ResponseEntity<ErrorDetails> handleInvalidProjectDetailsException(Exception ex, WebRequest request) throws Exception {
return handleEntityWithCause(ex, request);
}

@ExceptionHandler(AlreadyExistsException.class)
public final ResponseEntity<ErrorDetails> handleAlreadyExistsException(Exception ex, WebRequest request) throws Exception {
return handleEntityWithoutCause(ex, request);
}

@ExceptionHandler(NotFoundException.class)
public final ResponseEntity<ErrorDetails> handleNotFoundException(Exception ex, WebRequest request) throws Exception {
return handleEntityWithoutCause(ex, request);
}

@ExceptionHandler(InvalidNotificationDetailsException.class)
public final ResponseEntity<ErrorDetails> handleInvalidNotificationDetailsException(Exception ex, WebRequest request) {
return handleEntityWithoutCause(ex, request);
}

@ExceptionHandler(InvalidUserDetailsException.class)
public final ResponseEntity<ErrorDetails> handleInvalidUserDetailsException(Exception ex, WebRequest request) {
return handleEntityWithCause(ex, request);
}

public ResponseEntity<ErrorDetails> handleEntityWithCause(Exception ex, WebRequest request) {
String cause = ex.getCause() != null ? ex.getCause().getMessage() : null;
HttpStatus status = ex.getClass().getAnnotation(ResponseStatus.class).value();
ErrorDetails error;
if (cause != null) {
error = new ErrorDetailsWithCause(Instant.now(), status.value(), cause, ex.getMessage(), request.getDescription(false));
} else {
error = new ErrorDetails(Instant.now(), status.value(), ex.getMessage(), request.getDescription(false));
}
return new ResponseEntity<>(error, status);
}

public ResponseEntity<ErrorDetails> handleEntityWithoutCause(Exception ex, WebRequest request) {
HttpStatus status = ex.getClass().getAnnotation(ResponseStatus.class).value();
ErrorDetails error = new ErrorDetails(Instant.now(), status.value(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(error, status);
}
}

0 comments on commit 6a185fb

Please sign in to comment.