Adding statusCode from AppsmithPluginErrors when plugins error out

This ensures that we can always display a status code on the client.
This commit is contained in:
Arpit Mohan 2020-05-26 11:50:09 +00:00
parent a569156029
commit 9f82bde92c
6 changed files with 57 additions and 2 deletions

View File

@ -4,3 +4,9 @@ target/**
**/.idea
**/target
**/dist
.settings/**
**/.settings
**/.classpath
**/.project
**/.factorypath
**.iml

View File

@ -79,7 +79,7 @@ public class AclService {
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
WebClient.RequestHeadersSpec<?> request = webClient.post().syncBody(requestBody.toString());
WebClient.RequestHeadersSpec<?> request = webClient.post().bodyValue(requestBody.toString());
return request.retrieve().bodyToMono(OpaResponse.class);
});
}

View File

@ -10,6 +10,7 @@ import lombok.ToString;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Map;
import java.util.Set;
@Getter
@ -35,6 +36,11 @@ public class Action extends BaseDomain {
Boolean executeOnLoad;
/* This is a list of fields specified by the client to signify which fields have dynamic bindings in them.
TODO: The server can use this field to simplify our Mustache substitutions in the future
*/
Map<String, Boolean> dynamicBindings;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
Boolean isValid;

View File

@ -8,6 +8,8 @@ import com.appsmith.external.models.PaginationType;
import com.appsmith.external.models.Param;
import com.appsmith.external.models.Property;
import com.appsmith.external.models.Provider;
import com.appsmith.external.pluginExceptions.AppsmithPluginError;
import com.appsmith.external.pluginExceptions.AppsmithPluginException;
import com.appsmith.external.plugins.PluginExecutor;
import com.appsmith.server.constants.AnalyticsEvents;
import com.appsmith.server.constants.FieldName;
@ -432,6 +434,12 @@ public class ActionServiceImpl extends BaseService<ActionRepository, Action, Str
ActionExecutionResult result = new ActionExecutionResult();
result.setBody(e.getMessage());
result.setIsExecutionSuccess(false);
// Set the status code for Appsmith plugin errors
if (e instanceof AppsmithPluginException) {
result.setStatusCode(((AppsmithPluginException) e).getAppErrorCode().toString());
} else {
result.setStatusCode(AppsmithPluginError.PLUGIN_ERROR.getAppErrorCode().toString());
}
return Mono.just(result);
})
.map(obj -> populateRequestFields(actionConfiguration, (ActionExecutionResult) obj));

View File

@ -203,7 +203,7 @@ public class MarketplaceServiceImpl implements MarketplaceService {
if (params != null) {
for (String key : params.keySet()) {
uriBuilder.queryParam(key, URLEncoder.encode(params.getFirst(key)));
uriBuilder.queryParam(key, URLEncoder.encode(params.getFirst(key), StandardCharsets.UTF_8));
}
}

View File

@ -3,6 +3,8 @@ package com.appsmith.server.services;
import com.appsmith.external.models.ActionConfiguration;
import com.appsmith.external.models.ActionExecutionResult;
import com.appsmith.external.models.Property;
import com.appsmith.external.pluginExceptions.AppsmithPluginError;
import com.appsmith.external.pluginExceptions.AppsmithPluginException;
import com.appsmith.external.plugins.PluginExecutor;
import com.appsmith.server.constants.FieldName;
import com.appsmith.server.domains.Action;
@ -301,6 +303,39 @@ public class ActionServiceTest {
.verifyComplete();
}
@Test
@WithUserDetails(value = "api_user")
public void testActionExecuteErrorResponse() {
ActionExecutionResult mockResult = new ActionExecutionResult();
mockResult.setIsExecutionSuccess(true);
mockResult.setBody("response-body");
Action action = new Action();
ActionConfiguration actionConfiguration = new ActionConfiguration();
actionConfiguration.setHeaders(List.of(
new Property("random-header-key", "random-header-value"),
new Property("", "")
));
action.setActionConfiguration(actionConfiguration);
ExecuteActionDTO executeActionDTO = new ExecuteActionDTO();
executeActionDTO.setAction(action);
AppsmithPluginException pluginException = new AppsmithPluginException(AppsmithPluginError.PLUGIN_ERROR);
Mockito.when(pluginExecutorHelper.getPluginExecutor(Mockito.any())).thenReturn(Mono.just(pluginExecutor));
Mockito.when(pluginExecutor.execute(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Mono.error(pluginException));
Mockito.when(pluginExecutor.datasourceCreate(Mockito.any())).thenReturn(Mono.empty());
Mono<ActionExecutionResult> executionResultMono = actionService.executeAction(executeActionDTO);
StepVerifier.create(executionResultMono)
.assertNext(result -> {
assertThat(result.getIsExecutionSuccess()).isFalse();
assertThat(result.getStatusCode()).isEqualTo(pluginException.getAppErrorCode().toString());
})
.verifyComplete();
}
private void executeAndAssertAction(ExecuteActionDTO executeActionDTO, ActionConfiguration actionConfiguration, ActionExecutionResult mockResult) {
Mono<ActionExecutionResult> actionExecutionResultMono = executeAction(executeActionDTO, actionConfiguration, mockResult);