Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions school/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,31 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<!-- <scope>runtime</scope>-->
<!-- <scope>runtime</scope>-->
</dependency>

<!-- PostgreSQL -->
<!-- <dependency>-->
<!-- <groupId>org.postgresql</groupId>-->
<!-- <artifactId>postgresql</artifactId>-->
<!-- </dependency>-->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Manual security dependencies -->

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.lambdaschool.school.exceptions;

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

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {

public ResourceNotFoundException(String message)
{
super(message);
}
public ResourceNotFoundException(String message, Throwable cause)
{
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

package com.lambdaschool.school.exceptions;

public class ValidationError {

private String code;
private String message;

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.lambdaschool.school.handlers;

import com.lambdaschool.school.exceptions.ResourceNotFoundException;
import com.lambdaschool.school.model.ErrorDetail;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpHeaders;
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.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler
{
@ExceptionHandler({ResourceNotFoundException.class})
public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request)
{
ErrorDetail errorDetail = new ErrorDetail();

errorDetail.setTimestamp(new Date().getTime());
errorDetail.setStatus(HttpStatus.NOT_FOUND.value());

errorDetail.setTitle("Resource Not Found");
errorDetail.setDetail(rnfe.getMessage());
errorDetail.setDeveloperMessage(rnfe.getClass().getName());

return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
}

@Override
protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
{
ErrorDetail errorDetail = new ErrorDetail();

errorDetail.setTimestamp(new Date().getTime());
errorDetail.setStatus(HttpStatus.NOT_FOUND.value());

errorDetail.setTitle("Path not found");
errorDetail.setDetail(request.getDescription(true));
errorDetail.setDeveloperMessage("Rest Handler Not Found (check for valid URI)");

return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
}


@Override
protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
{
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTimestamp(new Date().getTime());
errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());

errorDetail.setTitle("Type mismatch");
errorDetail.setDetail(ex.getMessage());
errorDetail.setDeveloperMessage(request.getDescription(true));

return new ResponseEntity<>(errorDetail, null, HttpStatus.BAD_REQUEST);
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.lambdaschool.school.model;

import com.lambdaschool.school.exceptions.ValidationError;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ErrorDetail {

private String title;
private int status;
private String detail;
private String timestamp;
private String developerMessage;
private Map<String, List<ValidationError>> errors = new HashMap<>();

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public String getDetail() {
return detail;
}

public void setDetail(String detail) {
this.detail = detail;
}

public String getTimestamp() {
return timestamp;
}

public void setTimestamp(Long timestamp) {
this.timestamp = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS").format(new Date(timestamp));
}

public String getDeveloperMessage() {
return developerMessage;
}

public void setDeveloperMessage(String developerMessage) {
this.developerMessage = developerMessage;
}

public Map<String, List<ValidationError>> getErrors() {
return errors;
}

public void setErrors(Map<String, List<ValidationError>> errors) {
this.errors = errors;
}
}