Skip to content

Commit

Permalink
Do not expose SQL exceptions to frontend
Browse files Browse the repository at this point in the history
- Catch all sql exceptions, print their stacktrace, and then return
a generic 500 error instead.
  • Loading branch information
Luke-Sikina committed Sep 10, 2020
1 parent d301429 commit d87af48
Showing 1 changed file with 10 additions and 0 deletions.
Expand Up @@ -5,6 +5,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand Down Expand Up @@ -181,4 +182,13 @@ public ResponseEntity<ErrorResponse> handleResourceDefinitionNotFound(ResourceDe
return new ResponseEntity<>(new ErrorResponse("Resource not found: " + ex.getResourceId()),
HttpStatus.NOT_FOUND);
}

@ExceptionHandler(BadSqlGrammarException.class)
public ResponseEntity<ErrorResponse> handleBadSqlGrammar(BadSqlGrammarException ex) {
ex.printStackTrace(); // we still want this to show up in the logs
return new ResponseEntity<>(
new ErrorResponse("SQL exception. If you are a maintainer of this instance, see logs for details."),
HttpStatus.INTERNAL_SERVER_ERROR
);
}
}

0 comments on commit d87af48

Please sign in to comment.