Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1596 - better error handling when converting excel input data to JSON object #1599

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 21 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"devDependencies": {
"@metahub/karma-jasmine-jquery": "^2.0.1",
"chromedriver": "^123.0.3",
"chromedriver": "^125.0.3",
"jasmine-core": "^3.5.0",
"jasmine-jquery": "^2.0.0",
"jquery": "^3.4.1",
Expand Down
Loading