Merge branch 'hotfix/log-execute-action' into 'release'

Added log for execute action

See merge request theappsmith/internal-tools-server!159
This commit is contained in:
Trisha Anand 2020-01-15 09:13:27 +00:00
commit a29ec30798
9 changed files with 102 additions and 18 deletions

View File

@ -0,0 +1,27 @@
package com.appsmith.external.pluginExceptions;
import lombok.Getter;
import java.text.MessageFormat;
@Getter
public enum AppsmithPluginError {
PLUGIN_ERROR(500, 5000, "PluginExecution failed with error {0}");
private Integer httpErrorCode;
private Integer appErrorCode;
private String message;
AppsmithPluginError(Integer httpErrorCode, Integer appErrorCode, String message, Object... args) {
this.httpErrorCode = httpErrorCode;
this.appErrorCode = appErrorCode;
MessageFormat fmt = new MessageFormat(message);
this.message = fmt.format(args);
}
public String getMessage(Object... args) {
return new MessageFormat(this.message).format(args);
}
}

View File

@ -0,0 +1,35 @@
package com.appsmith.external.pluginExceptions;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class AppsmithPluginException extends Exception {
private AppsmithPluginError error;
private Object[] args;
public AppsmithPluginException(String msg) {
super(msg);
}
public AppsmithPluginException(AppsmithPluginError error, Object... args) {
super(error.getMessage(args));
this.error = error;
this.args = args;
}
public Integer getHttpStatus() {
return this.error.getHttpErrorCode();
}
public String getMessage(Object... args) {
return this.error.getMessage(this.args);
}
public Integer getAppErrorCode() {
return this.error.getAppErrorCode();
}
}

View File

@ -1,7 +1,6 @@
package com.appsmith.external.plugins; package com.appsmith.external.plugins;
import com.appsmith.external.models.ActionConfiguration; import com.appsmith.external.models.ActionConfiguration;
import com.appsmith.external.models.ActionExecutionResult;
import com.appsmith.external.models.DatasourceConfiguration; import com.appsmith.external.models.DatasourceConfiguration;
import org.pf4j.ExtensionPoint; import org.pf4j.ExtensionPoint;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -16,7 +15,7 @@ public interface PluginExecutor extends ExtensionPoint {
* @param actionConfiguration : These are the configurations which have been used to create an Action from a Datasource. * @param actionConfiguration : These are the configurations which have been used to create an Action from a Datasource.
* @return ActionExecutionResult : This object is returned to the user which contains the result values from the execution. * @return ActionExecutionResult : This object is returned to the user which contains the result values from the execution.
*/ */
Mono<ActionExecutionResult> execute(Object connection, DatasourceConfiguration datasourceConfiguration, ActionConfiguration actionConfiguration); Mono<Object> execute(Object connection, DatasourceConfiguration datasourceConfiguration, ActionConfiguration actionConfiguration);
/** /**
* This function is responsible for creating the connection to the data source and returning the connection variable * This function is responsible for creating the connection to the data source and returning the connection variable

View File

@ -45,7 +45,7 @@ public class MongoPlugin extends BasePlugin {
* @return * @return
*/ */
@Override @Override
public Mono<ActionExecutionResult> execute(Object connection, public Mono<Object> execute(Object connection,
DatasourceConfiguration datasourceConfiguration, DatasourceConfiguration datasourceConfiguration,
ActionConfiguration actionConfiguration) { ActionConfiguration actionConfiguration) {

View File

@ -47,7 +47,7 @@ public class PostgresPlugin extends BasePlugin {
public static class PostgresPluginExecutor implements PluginExecutor { public static class PostgresPluginExecutor implements PluginExecutor {
@Override @Override
public Mono<ActionExecutionResult> execute(Object connection, public Mono<Object> execute(Object connection,
DatasourceConfiguration datasourceConfiguration, DatasourceConfiguration datasourceConfiguration,
ActionConfiguration actionConfiguration) { ActionConfiguration actionConfiguration) {

View File

@ -4,6 +4,8 @@ import com.appsmith.external.models.ActionConfiguration;
import com.appsmith.external.models.ActionExecutionResult; import com.appsmith.external.models.ActionExecutionResult;
import com.appsmith.external.models.DatasourceConfiguration; import com.appsmith.external.models.DatasourceConfiguration;
import com.appsmith.external.models.Property; import com.appsmith.external.models.Property;
import com.appsmith.external.pluginExceptions.AppsmithPluginError;
import com.appsmith.external.pluginExceptions.AppsmithPluginException;
import com.appsmith.external.plugins.BasePlugin; import com.appsmith.external.plugins.BasePlugin;
import com.appsmith.external.plugins.PluginExecutor; import com.appsmith.external.plugins.PluginExecutor;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
@ -41,7 +43,7 @@ public class RestApiPlugin extends BasePlugin {
public static class RestApiPluginExecutor implements PluginExecutor { public static class RestApiPluginExecutor implements PluginExecutor {
@Override @Override
public Mono<ActionExecutionResult> execute(Object connection, public Mono<Object> execute(Object connection,
DatasourceConfiguration datasourceConfiguration, DatasourceConfiguration datasourceConfiguration,
ActionConfiguration actionConfiguration) { ActionConfiguration actionConfiguration) {
@ -70,7 +72,7 @@ public class RestApiPlugin extends BasePlugin {
uri = createFinalUriWithQueryParams(url, actionConfiguration.getQueryParameters()); uri = createFinalUriWithQueryParams(url, actionConfiguration.getQueryParameters());
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();
return Mono.error(e); return Mono.error(new AppsmithPluginException(AppsmithPluginError.PLUGIN_ERROR, e));
} }
WebClient client = webClientBuilder.build(); WebClient client = webClientBuilder.build();
@ -95,12 +97,14 @@ public class RestApiPlugin extends BasePlugin {
headerInJsonString = objectMapper.writeValueAsString(headers); headerInJsonString = objectMapper.writeValueAsString(headers);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); e.printStackTrace();
return Mono.defer(() -> Mono.error(new AppsmithPluginException(AppsmithPluginError.PLUGIN_ERROR, e)));
} }
try { try {
// Set headers in the result now // Set headers in the result now
result.setHeaders(objectMapper.readTree(headerInJsonString)); result.setHeaders(objectMapper.readTree(headerInJsonString));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return Mono.defer(() -> Mono.error(new AppsmithPluginException(AppsmithPluginError.PLUGIN_ERROR, e)));
} }
// Find the media type of the response to parse the body as required. Currently only JSON // Find the media type of the response to parse the body as required. Currently only JSON
@ -119,6 +123,7 @@ public class RestApiPlugin extends BasePlugin {
result.setBody(objectMapper.readTree(body)); result.setBody(objectMapper.readTree(body));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return Mono.defer(() -> Mono.error(new AppsmithPluginException(AppsmithPluginError.PLUGIN_ERROR, e)));
} }
} else { } else {
// If the body is not of JSON type, just set it as is. // If the body is not of JSON type, just set it as is.
@ -126,7 +131,8 @@ public class RestApiPlugin extends BasePlugin {
} }
} }
return result; return result;
}); })
.doOnError(e -> Mono.error(new AppsmithPluginException(AppsmithPluginError.PLUGIN_ERROR, e)));
} }
private Mono<ClientResponse> httpCall(WebClient webClient, HttpMethod httpMethod, URI uri, String requestBody, int iteration) { private Mono<ClientResponse> httpCall(WebClient webClient, HttpMethod httpMethod, URI uri, String requestBody, int iteration) {
@ -139,7 +145,9 @@ public class RestApiPlugin extends BasePlugin {
.uri(uri) .uri(uri)
.body(BodyInserters.fromObject(requestBody)) .body(BodyInserters.fromObject(requestBody))
.exchange() .exchange()
.flatMap(response -> { .doOnError(e -> Mono.error(new AppsmithPluginException(AppsmithPluginError.PLUGIN_ERROR, e)))
.flatMap(res -> {
ClientResponse response = (ClientResponse) res;
if (response.statusCode().is3xxRedirection()) { if (response.statusCode().is3xxRedirection()) {
String redirectUrl = response.headers().header("Location").get(0); String redirectUrl = response.headers().header("Location").get(0);
URI redirectUri = null; URI redirectUri = null;

View File

@ -1,5 +1,6 @@
package com.appsmith.server.exceptions; package com.appsmith.server.exceptions;
import com.appsmith.external.pluginExceptions.AppsmithPluginException;
import com.appsmith.server.dtos.ResponseDTO; import com.appsmith.server.dtos.ResponseDTO;
import com.rollbar.notifier.Rollbar; import com.rollbar.notifier.Rollbar;
import com.segment.analytics.Analytics; import com.segment.analytics.Analytics;
@ -77,6 +78,16 @@ public class GlobalExceptionHandler {
AppsmithError.GENERIC_BAD_REQUEST.getMessage(e.getReason())))); AppsmithError.GENERIC_BAD_REQUEST.getMessage(e.getReason()))));
} }
@ExceptionHandler
@ResponseBody
public Mono<ResponseDTO<ErrorDTO>> catchPluginException(AppsmithPluginException e, ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
log.error("", e);
rollbar.log(e);
return Mono.just(new ResponseDTO<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), new ErrorDTO(AppsmithError.INTERNAL_SERVER_ERROR.getHttpErrorCode(),
e.getLocalizedMessage())));
}
/** /**
* This function catches the generic Exception class and is meant to be a catch all to ensure that we don't leak * This function catches the generic Exception class and is meant to be a catch all to ensure that we don't leak
* any information to the client. Ideally, the function #catchAppsmithException should be used * any information to the client. Ideally, the function #catchAppsmithException should be used

View File

@ -370,7 +370,11 @@ public class ActionServiceImpl extends BaseService<ActionRepository, Action, Str
actionConfiguration = action.getActionConfiguration(); actionConfiguration = action.getActionConfiguration();
} }
Integer timeoutDuration = actionConfiguration.getTimeoutInMillisecond(); Integer timeoutDuration = actionConfiguration.getTimeoutInMillisecond();
log.debug("Got the timeoutDuration to be: {} ms for action: {}", timeoutDuration, action.getName());
log.debug("Execute Action called in Page {}, for action id : {} action name : {}, {}, {}",
action.getPageId(), action.getId(), action.getName(), datasourceConfiguration,
actionConfiguration);
return datasourceContextService return datasourceContextService
.getDatasourceContext(datasource) .getDatasourceContext(datasource)
//Now that we have the context (connection details, execute the action //Now that we have the context (connection details, execute the action
@ -381,7 +385,8 @@ public class ActionServiceImpl extends BaseService<ActionRepository, Action, Str
.timeout(Duration.ofMillis(timeoutDuration)); .timeout(Duration.ofMillis(timeoutDuration));
})) }))
.flatMap(obj -> obj) .flatMap(obj -> obj)
.flatMap(result -> { .flatMap(res -> {
ActionExecutionResult result = (ActionExecutionResult) res;
Mono<ActionExecutionResult> resultMono = Mono.just(result); Mono<ActionExecutionResult> resultMono = Mono.just(result);
if (actionFromDto.getId() == null) { if (actionFromDto.getId() == null) {
// This is a dry-run. We shouldn't query the db because it'll throw NPE on null IDs // This is a dry-run. We shouldn't query the db because it'll throw NPE on null IDs

View File

@ -1,7 +1,6 @@
package com.appsmith.server.services; package com.appsmith.server.services;
import com.appsmith.external.models.ActionConfiguration; import com.appsmith.external.models.ActionConfiguration;
import com.appsmith.external.models.ActionExecutionResult;
import com.appsmith.external.models.DatasourceConfiguration; import com.appsmith.external.models.DatasourceConfiguration;
import com.appsmith.external.plugins.PluginExecutor; import com.appsmith.external.plugins.PluginExecutor;
import com.appsmith.server.constants.FieldName; import com.appsmith.server.constants.FieldName;
@ -44,7 +43,7 @@ public class DatasourceServiceTest {
class TestPluginExecutor implements PluginExecutor { class TestPluginExecutor implements PluginExecutor {
@Override @Override
public Mono<ActionExecutionResult> execute(Object connection, DatasourceConfiguration datasourceConfiguration, ActionConfiguration actionConfiguration) { public Mono<Object> execute(Object connection, DatasourceConfiguration datasourceConfiguration, ActionConfiguration actionConfiguration) {
System.out.println("In the execute"); System.out.println("In the execute");
return null; return null;
} }