Skip to content

Commit

Permalink
Throw exception when client attempts to annotate with unsupported lan…
Browse files Browse the repository at this point in the history
…guage.
  • Loading branch information
ledsoft committed Nov 13, 2024
1 parent bbf34ec commit b8bdbe7
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 2 deletions.
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public interface LemmatizerApi {
* @param text text to lemmatize
* @param lang language to use
* @return result of the lemmatizations
* @throws cz.cvut.kbss.textanalysis.exception.UnsupportedLanguageException If the given language is not supported
*/
LemmatizerResult process(String text, String lang);
}
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;
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import cz.cuni.mff.ufal.morphodita.TokenRanges;
import cz.cuni.mff.ufal.morphodita.Tokenizer;
import cz.cvut.kbss.annotace.configuration.MorphoditaConf;
import cz.cvut.kbss.textanalysis.exception.UnsupportedLanguageException;
import cz.cvut.kbss.textanalysis.lemmatizer.LemmatizerApi;
import cz.cvut.kbss.textanalysis.lemmatizer.model.LemmatizerResult;
import cz.cvut.kbss.textanalysis.lemmatizer.model.SingleLemmaResult;
Expand Down Expand Up @@ -77,7 +78,7 @@ public LemmatizerResult process(String s, String lang) {

Tagger tagger = taggers.get(lang);
if (tagger == null) {
throw new RuntimeException("No tagger for language " + lang + " available.");
throw new UnsupportedLanguageException("No tagger for language '" + lang + "' available.");
}

while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.johnsnowlabs.nlp.annotators.sbd.pragmatic.SentenceDetector;
import com.johnsnowlabs.nlp.pretrained.PretrainedPipeline;
import cz.cvut.kbss.annotace.configuration.SparkConf;
import cz.cvut.kbss.textanalysis.exception.UnsupportedLanguageException;
import cz.cvut.kbss.textanalysis.lemmatizer.LemmatizerApi;
import cz.cvut.kbss.textanalysis.lemmatizer.model.LemmatizerResult;
import cz.cvut.kbss.textanalysis.lemmatizer.model.SingleLemmaResult;
Expand Down Expand Up @@ -112,7 +113,7 @@ public LemmatizerResult process(final String text, final String lang) {
final List<List<SingleLemmaResult>> results = new ArrayList<>();

for (final String label : labels) {
final Map<String, List<IAnnotation>> map = pipelines.get(lang).fullAnnotateJava(label);
final Map<String, List<IAnnotation>> map = getPipeline(lang).fullAnnotateJava(label);
final List<IAnnotation> tokens = map.get("token");
final List<SingleLemmaResult> res = new ArrayList<>();
results.add(res);
Expand Down Expand Up @@ -149,4 +150,11 @@ public LemmatizerResult process(final String text, final String lang) {
result.setLemmatizer(this.getClass().getName());
return result;
}

private LightPipeline getPipeline(String lang) {
if (!pipelines.containsKey(lang)) {
throw new UnsupportedLanguageException("No pipeline found for language '" + lang + "'");
}
return pipelines.get(lang);
}
}

0 comments on commit b8bdbe7

Please sign in to comment.