Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- better error handling when converting excel input data to JSON object
  • Loading branch information
temi committed Jun 7, 2024
1 parent 355fc91 commit d63fa08
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
4 changes: 4 additions & 0 deletions grails-app/assets/javascripts/bulk-import-view-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ function BulkUploadViewModel(activityImport) {
dataType: "json",
success: function (response) {
activityImport.dataToLoad(response.data);
},
error: function (jqXHR, status, error) {
var resp = jqXHR.responseJSON && jqXHR.responseJSON.error || "";
message('Failed to convert file to data <br/>' + resp);
}
});
return convertExcelToJSONRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.springframework.web.multipart.MultipartFile

import static org.apache.http.HttpStatus.SC_BAD_REQUEST
import static org.apache.http.HttpStatus.SC_OK
import static org.apache.http.HttpStatus.SC_INTERNAL_SERVER_ERROR

@SecurityScheme(name = "auth",
type = SecuritySchemeType.HTTP,
Expand Down Expand Up @@ -1306,10 +1307,14 @@ class BioActivityController {

if (pActivityId && type && file) {
def content = activityService.convertExcelToOutputData(pActivityId, type, file)
render text: content as JSON
def status = SC_OK
if (content.error) {
status = SC_INTERNAL_SERVER_ERROR
}
render text: content as JSON, status: status
}
else {
render text: [message: "Missing required parameters - pActivityId, type & data (excel file)"] as JSON, status: HttpStatus.SC_BAD_REQUEST
render text: [message: "Missing required parameters - pActivityId, type & data (excel file)"] as JSON, status: SC_BAD_REQUEST
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,12 @@ class ActivityService {
}

def convertExcelToOutputData(String id, String type, def file){
def result = webService.postMultipart(grailsApplication.config.ecodata.service.url + "/metadata/extractOutputDataFromActivityExcelTemplate", [pActivityId: id, type: type], file, 'data')
result.content?.subMap('data') ?: result
def result = webService.postMultipart(grailsApplication.config.ecodata.service.url + "/metadata/extractOutputDataFromActivityExcelTemplate", [pActivityId: id, type: type], file, 'data', false, true)
if (result.error) {
return result.details
}
else {
return result.content?.subMap('data') ?: result
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,12 @@ class WebService {
* @param url the URL to forward to.
* @param params the (string typed) HTTP parameters to be attached.
* @param file the Multipart file object to forward.
* @param includeFailureDetails if true, the return value will include response body. If content type is JSON, an object will be returned in `details` property.
* @return [status:<request status>, content:<The response content from the server, assumed to be JSON>
*/
def postMultipart(url, Map params, MultipartFile file, fileParam = 'files', boolean useToken = false) {
def postMultipart(url, Map params, MultipartFile file, fileParam = 'files', boolean useToken = false, boolean includeFailureDetails = false) {

postMultipart(url, params, file.inputStream, file.contentType, file.originalFilename, fileParam, useToken)
postMultipart(url, params, file.inputStream, file.contentType, file.originalFilename, fileParam, useToken, includeFailureDetails)
}

/**
Expand All @@ -460,7 +461,7 @@ class WebService {
* @param fileParamName the name of the HTTP parameter that will be used for the post.
* @return [status:<request status>, content:<The response content from the server, assumed to be JSON>
*/
def postMultipart(url, Map params, InputStream contentIn, contentType, originalFilename, fileParamName = 'files', boolean useToken = false) {
def postMultipart(url, Map params, InputStream contentIn, contentType, originalFilename, fileParamName = 'files', boolean useToken = false, boolean includeFailureDetails = false) {

def result = [:]
def user = userService.getUser()
Expand Down Expand Up @@ -496,9 +497,11 @@ class WebService {
result.content = message
}

response.failure = {resp ->
response.failure = {resp, reader ->
result.status = resp.status
result.error = "Error POSTing to ${url}"
if (includeFailureDetails)
result.details = reader
}
}
result
Expand Down

0 comments on commit d63fa08

Please sign in to comment.