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
9 changes: 7 additions & 2 deletions schools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lambdaschool</groupId>
Expand All @@ -15,7 +15,7 @@
<description>Demo project for Spring Boot</description>

<properties>
<java.version>11</java.version>
<java.version>14</java.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -43,6 +43,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.20</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.lambdaschool.schools.controllers;

import com.lambdaschool.schools.models.ErrorDetail;
import com.lambdaschool.schools.models.Student;
import com.lambdaschool.schools.services.StudentService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -129,6 +134,26 @@ public ResponseEntity<?> updateFullstudent(
return new ResponseEntity<>(HttpStatus.OK);
}

// @ApiOperation(value = "returns the student with the given name",
// response = Student.class)
// @ApiResponses(value = {@ApiResponse(code = 200,
// message = "Student Found",
// response = Student.class), @ApiResponse(code = 404,
// message = "Student Not Found",
// response = ErrorDetail.class)})
// @GetMapping(value = "/user/name/{name}",
// produces = {"application/json"})
// public ResponseEntity<?> getStudentByName(
// @ApiParam(value = "student name",
// required = true,
// example = "johnmitchell")
// @PathVariable
// String name)
// {
// Student s = studentService.findByName(name);
// return new ResponseEntity<>(s,
// HttpStatus.OK);
// }
/**
* Deletes a given student along with associated student course enrollments
* <br>Example: <a href="http://localhost:2019/students/student/14">http://localhost:2019/students/students/14</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.lambdaschool.schools.exceptions;

import com.lambdaschool.schools.handlers.HelperFunctions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.LinkedHashMap;
import java.util.Map;
/*
private String title; // title of exception
private int status; // Http method status as a number ex 404
private String detail; // details to the client
private Date timestamp; // time of exception
private String developerMessage; // message to developer if needed
private List<ValidationError> errors = new ArrayList<>(); // list of errors
*/

@Component
public class CustomErrorDetails extends DefaultErrorAttributes
{

@Autowired
private HelperFunctions helper;

@Override
public Map<String, Object> getErrorAttributes(
WebRequest webRequest,
boolean includeStackTrace)
{
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest,
includeStackTrace);

Map<String, Object> errorDetails = new LinkedHashMap<>(); // these will match the fields from ErrorDetail
errorDetails.put("title", errorAttributes.get("error"));
errorDetails.put("status", errorAttributes.get("status"));
errorDetails.put("detail", errorAttributes.get("message"));
errorDetails.put("timestamp", errorAttributes.get("timestamp"));
errorDetails.put("developerMessage", "path: " + errorAttributes.get("path"));
errorDetails.put("errors", helper.getConstraintViolation(this.getError(webRequest)));

return errorDetails;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lambdaschool.schools.exceptions;

public class ResourceFoundException extends RuntimeException
{
public ResourceFoundException(String message)
{
super("Found an issue with School: " + message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lambdaschool.schools.exceptions;

public class ResourceNotFoundException extends RuntimeException
{
public ResourceNotFoundException(String message)
{
super("Found an issue with School: " + message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lambdaschool.schools.exceptions;

public class URLNotFoundException extends RuntimeException
{
public URLNotFoundException(String message)
{
super("Found an issue with School: " + message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.lambdaschool.schools.handlers;

import com.lambdaschool.schools.models.ValidationError;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.ArrayList;
import java.util.List;

@Component
public class HelperFunctions
{
public List<ValidationError> getConstraintViolation(Throwable cause)
{
while ((cause != null) && !(cause instanceof ConstraintViolationException))
{
cause = cause.getCause();
}

List<ValidationError> listVE = new ArrayList<>();

if (cause != null)
{
ConstraintViolationException ex = (ConstraintViolationException) cause; // typecasting cause
for (ConstraintViolation cv : ex.getConstraintViolations())
{
ValidationError newVE = new ValidationError();
newVE.setCode(cv.getInvalidValue().toString());
newVE.setMessage((cv.getMessage()));
listVE.add(newVE);
}
}
return listVE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.lambdaschool.schools.handlers;

import com.lambdaschool.schools.exceptions.ResourceFoundException;
import com.lambdaschool.schools.exceptions.ResourceNotFoundException;
import com.lambdaschool.schools.models.ErrorDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.util.Date;

@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler
{

@Autowired
private HelperFunctions helper;

public RestExceptionHandler()
{
super();
}

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe)
{
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTimestamp(new Date());
errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
errorDetail.setTitle("Resource Not Found");
errorDetail.setDetail(rnfe.getMessage()); // message from ResourceNotFound class
errorDetail.setDeveloperMessage(rnfe.getClass().getName());
errorDetail.setErrors(helper.getConstraintViolation(rnfe));

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

@ExceptionHandler(ResourceFoundException.class)
public ResponseEntity<?> handleResourceFoundException(ResourceFoundException rfe)
{
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTimestamp(new Date());
errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
errorDetail.setTitle("Resource Not Found");
errorDetail.setDetail(rfe.getMessage()); // message from ResourceNotFound class
errorDetail.setDeveloperMessage(rfe.getClass().getName());
errorDetail.setErrors(helper.getConstraintViolation(rfe));

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

@Override
protected ResponseEntity<Object> handleExceptionInternal(
Exception ex,
Object body,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
{
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTimestamp(new Date());
errorDetail.setTitle("Rest Internal Exception");
errorDetail.setDetail(ex.getMessage());
errorDetail.setDeveloperMessage(ex.getClass().getName());
errorDetail.setErrors(helper.getConstraintViolation(ex));

return new ResponseEntity<>(errorDetail, null, status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ abstract class Auditable
@LastModifiedDate
@Temporal(TIMESTAMP)
protected Date lastModifiedDate;

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* The entity allowing interaction with the courses table
*/
@Entity
@Table(name = "courses")
@Table(name = "courses") // Driving table
public class Course
extends Auditable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.lambdaschool.schools.models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ErrorDetail
{
// this field layout is specific to our program/company we're working for.
private String title; // title of exception
private int status; // Http method status as a number ex 404
private String detail; // details to the client
private Date timestamp; // time of exception
private String developerMessage; // message to developer if needed
private List<ValidationError> errors = new ArrayList<>(); // list of errors

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 Date getTimestamp()
{
return timestamp;
}

public void setTimestamp(Date timestamp)
{
this.timestamp = timestamp;
}

public String getDeveloperMessage()
{
return developerMessage;
}

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

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

public void setErrors(List<ValidationError> errors)
{
this.errors = errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ public void setCourses(List<StudCourses> courses)
{
this.courses = courses;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.lambdaschool.schools.models;

public class ValidationError
{
private String code; // the name of the field that caused the error.
private String message; // send the list to fix

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;
}

@Override
public String toString()
{
return "ValidationError{" +
"code='" + code + '\'' +
", message='" + message + '\'' +
'}';
}
}
Loading