Skip to content

Commit

Permalink
Add an understandable description of errors in the Rest API #2605
Browse files Browse the repository at this point in the history
(cherry picked from commit 0a369dd)
  • Loading branch information
NikitaShchienko committed Jan 19, 2024
1 parent 3757723 commit 6cb96af
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import io.jmix.reports.yarg.reporting.ReportOutputDocument;
import io.jmix.reports.yarg.util.converter.ObjectToStringConverter;
import io.jmix.core.*;
import io.jmix.core.metamodel.model.MetaClass;
import io.jmix.core.metamodel.model.MetaProperty;
Expand All @@ -31,16 +29,19 @@
import io.jmix.reports.entity.*;
import io.jmix.reports.exception.FailedToConnectToOpenOfficeException;
import io.jmix.reports.exception.NoOpenOfficeFreePortsException;
import io.jmix.reports.exception.ReportParametersValidationException;
import io.jmix.reports.exception.ReportingException;
import io.jmix.reports.runner.ReportRunContext;
import io.jmix.reports.runner.ReportRunner;
import io.jmix.reports.yarg.reporting.ReportOutputDocument;
import io.jmix.reports.yarg.util.converter.ObjectToStringConverter;
import io.jmix.security.constraint.PolicyStore;
import io.jmix.security.constraint.SecureOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import org.springframework.lang.Nullable;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -151,36 +152,29 @@ public ReportRestResult runReport(String entityId, String bodyJson) {
checkReportOutputType(report.getDefaultTemplate());
}
Map<String, Object> preparedValues = prepareValues(report, body.parameters);
if (body.template != null) {
try {
try {
if (body.template != null) {
ReportOutputDocument document = reportRunner.byReportEntity(report)
.withTemplateCode(body.template)
.withParams(preparedValues)
.run();

return new ReportRestResult(document, body.attachment);
} catch (FailedToConnectToOpenOfficeException e) {
throw new RestAPIException("Run report error", "Couldn't find LibreOffice instance",
HttpStatus.INTERNAL_SERVER_ERROR);
} catch (NoOpenOfficeFreePortsException e) {
throw new RestAPIException("Run report error", "Couldn't connect to LibreOffice instance. No free ports available.",
HttpStatus.INTERNAL_SERVER_ERROR);
} catch (ReportingException e) {
throw new RestAPIException("Run report error",
e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
} else {
try {
} else {
return new ReportRestResult(reportRunner.run(new ReportRunContext(report).setParams(preparedValues)), body.attachment);
} catch (FailedToConnectToOpenOfficeException e) {
throw new RestAPIException("Run report error", "Couldn't find LibreOffice instance",
HttpStatus.INTERNAL_SERVER_ERROR);
} catch (NoOpenOfficeFreePortsException e) {
throw new RestAPIException("Run report error", "Couldn't connect to LibreOffice instance. No free ports available.",
HttpStatus.INTERNAL_SERVER_ERROR);
} catch (ReportingException e) {
throw new RestAPIException("Run report error",
e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
} catch (FailedToConnectToOpenOfficeException e) {
throw new RestAPIException("Run report error", "Couldn't find LibreOffice instance",
HttpStatus.INTERNAL_SERVER_ERROR);
} catch (NoOpenOfficeFreePortsException e) {
throw new RestAPIException("Run report error", "Couldn't connect to LibreOffice instance. No free ports available.",
HttpStatus.INTERNAL_SERVER_ERROR);
} catch (ReportParametersValidationException e) {
throw new RestAPIException("Run report error",
e.getMessage(), HttpStatus.BAD_REQUEST);
} catch (ReportingException e) {
throw new RestAPIException("Run report error",
e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
package io.jmix.reports.yarg.reporting;

import com.google.common.base.Preconditions;
import io.jmix.reports.yarg.structure.*;
import io.jmix.reports.yarg.util.converter.ObjectToStringConverterImpl;
import io.jmix.reports.exception.ReportParametersValidationException;
import io.jmix.reports.yarg.exception.ReportingException;
import io.jmix.reports.yarg.exception.ReportingInterruptedException;
import io.jmix.reports.yarg.exception.ValidationException;
import io.jmix.reports.yarg.formatters.ReportFormatter;
import io.jmix.reports.yarg.formatters.factory.FormatterFactoryInput;
import io.jmix.reports.yarg.formatters.factory.ReportFormatterFactory;
import io.jmix.reports.yarg.loaders.factory.ReportLoaderFactory;
import io.jmix.reports.yarg.structure.*;
import io.jmix.reports.yarg.util.converter.ObjectToStringConverter;
import io.jmix.reports.yarg.util.converter.ObjectToStringConverterImpl;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -157,7 +158,7 @@ protected Map<String, Object> handleParameters(Report report, Map<String, Object
}

if (Boolean.TRUE.equals(reportParameter.getRequired()) && parameterValue == null) {
throw new IllegalArgumentException(format("Required report parameter \"%s\" not found", paramName));
throw new ReportParametersValidationException(format("Required report parameter \"%s\" not found", paramName));
}

if (!handledParams.containsKey(paramName)) {//make sure map contains all user parameters, even if value == null
Expand Down Expand Up @@ -225,7 +226,7 @@ protected String resolveOutputFileName(RunParams runParams, BandData rootBand) {
}

if (ReportOutputType.custom != reportTemplate.getOutputType()) {
ReportOutputType finalOutputType = (outputType != null ) ? outputType : reportTemplate.getOutputType();
ReportOutputType finalOutputType = (outputType != null) ? outputType : reportTemplate.getOutputType();
outputName = format("%s.%s", StringUtils.substringBeforeLast(outputName, "."), finalOutputType.getId());
}

Expand Down

0 comments on commit 6cb96af

Please sign in to comment.