-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw exception when client attempts to annotate with unsupported lan…
…guage.
- Loading branch information
Showing
7 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
api/src/main/java/cz/cvut/kbss/textanalysis/exception/AnnotaceException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Annotace | ||
* Copyright (C) 2024 Czech Technical University in Prague | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package cz.cvut.kbss.textanalysis.exception; | ||
|
||
/** | ||
* Top level application-specific exception. | ||
*/ | ||
public class AnnotaceException extends RuntimeException { | ||
|
||
public AnnotaceException(String message) { | ||
super(message); | ||
} | ||
|
||
public AnnotaceException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public AnnotaceException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
api/src/main/java/cz/cvut/kbss/textanalysis/exception/UnsupportedLanguageException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Annotace | ||
* Copyright (C) 2024 Czech Technical University in Prague | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package cz.cvut.kbss.textanalysis.exception; | ||
|
||
/** | ||
* Indicates that the client request analysis in an unsupported language. | ||
*/ | ||
public class UnsupportedLanguageException extends AnnotaceException { | ||
|
||
public UnsupportedLanguageException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
core/src/main/java/cz/cvut/kbss/textanalysis/rest/handler/ErrorInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Annotace | ||
* Copyright (C) 2024 Czech Technical University in Prague | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package cz.cvut.kbss.textanalysis.rest.handler; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* Contains information about an error and can be sent to client as JSON to let him know what is wrong. | ||
*/ | ||
@Setter | ||
@Getter | ||
public class ErrorInfo { | ||
|
||
/** | ||
* Readable message describing the error | ||
*/ | ||
private String message; | ||
|
||
private String requestUri; | ||
|
||
|
||
public ErrorInfo() { | ||
} | ||
|
||
public ErrorInfo(String requestUri) { | ||
this.requestUri = requestUri; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ErrorInfo{" + requestUri + ", message='" + message + "'}"; | ||
} | ||
|
||
/** | ||
* Creates a new instance with the specified message and request URI. | ||
* | ||
* @param message Error message | ||
* @param requestUri URI of the request which caused the error | ||
* @return New {@code ErrorInfo} instance | ||
*/ | ||
public static ErrorInfo createWithMessage(String message, String requestUri) { | ||
final ErrorInfo errorInfo = new ErrorInfo(requestUri); | ||
errorInfo.setMessage(message); | ||
return errorInfo; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
core/src/main/java/cz/cvut/kbss/textanalysis/rest/handler/RestExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Annotace | ||
* Copyright (C) 2024 Czech Technical University in Prague | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package cz.cvut.kbss.textanalysis.rest.handler; | ||
|
||
import cz.cvut.kbss.textanalysis.exception.AnnotaceException; | ||
import cz.cvut.kbss.textanalysis.exception.UnsupportedLanguageException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
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 javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* Exception handlers for REST controllers. | ||
* <p> | ||
* The general pattern should be that unless an exception can be handled in a more appropriate place it bubbles up to a | ||
* REST controller which originally received the request. There, it is caught by this handler, logged and a reasonable | ||
* error message is returned to the user. | ||
*/ | ||
@RestControllerAdvice | ||
public class RestExceptionHandler { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RestExceptionHandler.class); | ||
|
||
private static void logException(AnnotaceException ex, HttpServletRequest request) { | ||
LOG.error("Exception caught when processing request to '{}'.", request.getRequestURI(), ex); | ||
} | ||
|
||
private static ErrorInfo errorInfo(HttpServletRequest request, Throwable e) { | ||
return ErrorInfo.createWithMessage(e.getMessage(), request.getRequestURI()); | ||
} | ||
|
||
@ExceptionHandler(UnsupportedLanguageException.class) | ||
public ResponseEntity<ErrorInfo> unsupportedLanguageException(UnsupportedLanguageException ex, | ||
HttpServletRequest request) { | ||
logException(ex, request); | ||
return new ResponseEntity<>(errorInfo(request, ex), HttpStatus.CONFLICT); | ||
} | ||
|
||
@ExceptionHandler(AnnotaceException.class) | ||
public ResponseEntity<ErrorInfo> annotaceException(AnnotaceException ex, HttpServletRequest request) { | ||
logException(ex, request); | ||
return new ResponseEntity<>(errorInfo(request, ex), HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters