Skip to content

Commit

Permalink
✨ Requests are automatically aborted when any applicable Interceptor.…
Browse files Browse the repository at this point in the history
…resolve() method throws an Exception
  • Loading branch information
ujibang committed Feb 20, 2023
1 parent aecbc34 commit 10165e0
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*-
* ========================LICENSE_START=================================
* restheart-commons
* %%
* Copyright (C) 2019 - 2023 SoftInstigate
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/

package org.restheart.plugins;

import org.restheart.utils.HttpStatus;

/**
*
* @author Andrea Di Cesare {@literal <[email protected]>}
*/
public class InterceptorException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = -846615677223399751L;

int statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;

/**
*
*/
public InterceptorException() {
super();
}

/**
*
* @param statusCode
*/
public InterceptorException(int statusCode) {
super();
this.statusCode = statusCode;
}

/**
*
* @param message
*/
public InterceptorException(String message) {
super(message);
}

/**
*
* @param message
* @param statusCode
*/
public InterceptorException(String message, int statusCode) {
super(message);
this.statusCode = statusCode;
}

/**
*
* @param message
* @param cause
*/
public InterceptorException(String message, Throwable cause) {
super(message, cause);
}

/**
*
* @param message
* @param statusCode
* @param cause
*/
public InterceptorException(String message, int statusCode, Throwable cause) {
super(message, cause);
this.statusCode = statusCode;
}

/**
* @return the statusCode
*/
public int getStatusCode() {
return statusCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.restheart.exchange.Exchange;
import org.restheart.exchange.UninitializedRequest;
import org.restheart.exchange.UninitializedResponse;
import org.restheart.plugins.InterceptorException;
import org.restheart.plugins.PluginsRegistry;
import org.restheart.plugins.PluginsRegistryImpl;
import org.restheart.plugins.WildcardInterceptor;
Expand Down Expand Up @@ -103,9 +104,11 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
this.wildCardInterceptors.stream().filter(ri -> {
try {
return ri.resolve(request, response);
} catch (Exception e) {
LOGGER.warn("Error resolving interceptor {} for {} on intercept point {}", ri.getClass().getSimpleName(), exchange.getRequestPath(), REQUEST_BEFORE_EXCHANGE_INIT, e);
} catch (Exception ex) {
LOGGER.warn("Error resolving interceptor {} for {} on intercept point {}", ri.getClass().getSimpleName(), exchange.getRequestPath(), REQUEST_BEFORE_EXCHANGE_INIT, ex);

Exchange.setInError(exchange);
LambdaUtils.throwsSneakyException(new InterceptorException("Error resolving interceptor " + ri.getClass().getSimpleName(), ex));
return false;
}})
.forEachOrdered(ri -> {
Expand All @@ -116,7 +119,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
LOGGER.error("Error executing interceptor {} for {} on intercept point {}", PluginUtils.name(ri), exchange.getRequestPath(), REQUEST_BEFORE_EXCHANGE_INIT, ex);

Exchange.setInError(exchange);
LambdaUtils.throwsSneakyException(ex);
LambdaUtils.throwsSneakyException(new InterceptorException("Error executing interceptor " + ri.getClass().getSimpleName(), ex));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.restheart.exchange.ServiceResponse;
import org.restheart.plugins.InterceptPoint;
import org.restheart.plugins.Interceptor;
import org.restheart.plugins.InterceptorException;
import org.restheart.plugins.PluginsRegistry;
import org.restheart.plugins.PluginsRegistryImpl;
import org.restheart.plugins.Service;
Expand Down Expand Up @@ -105,9 +106,11 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
.filter(ri -> {
try {
return ri.resolve(request, response);
} catch (Exception e) {
LOGGER.warn("Error resolving interceptor {} for {} on intercept point {}", ri.getClass().getSimpleName(), exchange.getRequestPath(), interceptPoint, e);
} catch (Exception ex) {
LOGGER.warn("Error resolving interceptor {} for {} on intercept point {}", ri.getClass().getSimpleName(), exchange.getRequestPath(), interceptPoint, ex);

Exchange.setInError(exchange);
LambdaUtils.throwsSneakyException(new InterceptorException("Error resolving interceptor " + ri.getClass().getSimpleName(), ex));
return false;
}})
.forEachOrdered(ri -> {
Expand All @@ -119,7 +122,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
LOGGER.error("Error executing interceptor {} for {} on intercept point {}", PluginUtils.name(ri), exchange.getRequestPath(), interceptPoint, ex);

Exchange.setInError(exchange);
LambdaUtils.throwsSneakyException(ex);
LambdaUtils.throwsSneakyException(new InterceptorException("Error executing interceptor " + ri.getClass().getSimpleName(), ex));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.restheart.exchange.ServiceResponse;
import org.restheart.plugins.InterceptPoint;
import org.restheart.plugins.Interceptor;
import org.restheart.plugins.InterceptorException;
import org.restheart.plugins.PluginsRegistry;
import org.restheart.plugins.PluginsRegistryImpl;
import org.restheart.plugins.Service;
Expand Down Expand Up @@ -117,8 +118,11 @@ private void executeResponseInterceptor(HttpServerExchange exchange, Service han
.filter(ri -> !this.filterRequiringContent || !requiresContent(ri)).filter(ri -> {
try {
return ri.resolve(request, response);
} catch (Exception e) {
LOGGER.warn("Error resolving interceptor {} for {} on intercept point {}", ri.getClass().getSimpleName(), exchange.getRequestPath(), InterceptPoint.RESPONSE, e);
} catch (Exception ex) {
LOGGER.warn("Error resolving interceptor {} for {} on intercept point {}", ri.getClass().getSimpleName(), exchange.getRequestPath(), InterceptPoint.RESPONSE, ex);

Exchange.setInError(exchange);
LambdaUtils.throwsSneakyException(new InterceptorException("Error resolving interceptor " + ri.getClass().getSimpleName(), ex));
return false;
}
}).forEachOrdered(ri -> {
Expand All @@ -130,7 +134,7 @@ private void executeResponseInterceptor(HttpServerExchange exchange, Service han
LOGGER.error("Error executing interceptor {} for {} on intercept point {}", PluginUtils.name(ri), exchange.getRequestPath(), InterceptPoint.RESPONSE, ex);

Exchange.setInError(exchange);
LambdaUtils.throwsSneakyException(ex);
LambdaUtils.throwsSneakyException(new InterceptorException("Error executing interceptor " + ri.getClass().getSimpleName(), ex));
}
});
}
Expand All @@ -155,9 +159,11 @@ private void executeAsyncResponseInterceptor(HttpServerExchange exchange, Servic
.filter(ri -> {
try {
return ri.resolve(request, response);
} catch (Exception e) {
LOGGER.warn("Error resolving interceptor {} for {} on intercept point {}", ri.getClass().getSimpleName(), exchange.getRequestPath(), InterceptPoint.RESPONSE_ASYNC);
} catch (Exception ex) {
LOGGER.warn("Error resolving async interceptor {} for {} on intercept point {}", ri.getClass().getSimpleName(), exchange.getRequestPath(), InterceptPoint.RESPONSE_ASYNC);

Exchange.setInError(exchange);
LambdaUtils.throwsSneakyException(new InterceptorException("Error resolving async interceptor " + ri.getClass().getSimpleName(), ex));
return false;
}
})
Expand All @@ -168,10 +174,10 @@ private void executeAsyncResponseInterceptor(HttpServerExchange exchange, Servic
try {
ri.handle(request, response);
} catch (Exception ex) {
LOGGER.error("Error executing interceptor {} for {} on intercept point {}", PluginUtils.name(ri), exchange.getRequestPath(), InterceptPoint.RESPONSE_ASYNC, ex);
LOGGER.error("Error executing asyng interceptor {} for {} on intercept point {}", PluginUtils.name(ri), exchange.getRequestPath(), InterceptPoint.RESPONSE_ASYNC, ex);

Exchange.setInError(exchange);
LambdaUtils.throwsSneakyException(ex);
LambdaUtils.throwsSneakyException(new InterceptorException("Error executing async interceptor " + ri.getClass().getSimpleName(), ex));
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private boolean isContentRequired(HttpServerExchange exchange, InterceptPoint in
try {
return ri.resolve(request, response);
} catch (Exception e) {
LOGGER.warn("Error resolving interceptor {} for {} on intercept point {}", ri.getClass().getSimpleName(), exchange.getRequestPath(), interceptPoint, e);
LOGGER.warn("Error resolving interceptor {} for {} on intercept point {} to check if the content is required; assuming requiresContent=false", ri.getClass().getSimpleName(), exchange.getRequestPath(), interceptPoint, e);
return false;
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,16 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
}

mrOutput.into(_data);
} catch (MongoCommandException | InvalidMetadataException ex) {
response.setInError(HttpStatus.SC_INTERNAL_SERVER_ERROR, "error executing mapReduce", ex);
} catch (MongoCommandException ex) {
response.setInError(HttpStatus.SC_UNPROCESSABLE_ENTITY, "error executing mapReduce", ex);
LOGGER.error("error executing mapReduce /{}/{}/_aggrs/{}", request.getDBName(), request.getCollectionName(), queryUri, ex);
next(exchange);
return;
} catch(InvalidMetadataException ex) {
response.setInError(HttpStatus.SC_UNPROCESSABLE_ENTITY, "invalid aggregation", ex);
LOGGER.error("invalid mapReduce /{}/{}/_aggrs/{}", request.getDBName(), request.getCollectionName(), queryUri, ex);
next(exchange);
return;
} catch (QueryVariableNotBoundException qvnbe) {
response.setInError(HttpStatus.SC_BAD_REQUEST, "error executing mapReduce: " + qvnbe.getMessage());
LOGGER.error("error executing mapReduce /{}/{}/_aggrs/{}", request.getDBName(), request.getCollectionName(), queryUri, qvnbe);
Expand Down Expand Up @@ -170,12 +175,12 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {

agrOutput.into(_data);
} catch (MongoCommandException mce) {
response.setInError(HttpStatus.SC_INTERNAL_SERVER_ERROR, "error executing aggregation", mce);
response.setInError(HttpStatus.SC_UNPROCESSABLE_ENTITY, "error executing aggregation", mce);
LOGGER.error("error executing aggregation /{}/{}/_aggrs/{}: {}", request.getDBName(), request.getCollectionName(), queryUri, mongoCommandExceptionError(mce));
next(exchange);
return;
} catch(InvalidMetadataException ex) {
response.setInError(HttpStatus.SC_INTERNAL_SERVER_ERROR, "invalid aggregation", ex);
response.setInError(HttpStatus.SC_UNPROCESSABLE_ENTITY, "invalid aggregation", ex);
LOGGER.error("invalid aggregation /{}/{}/_aggrs/{}", request.getDBName(), request.getCollectionName(), queryUri, ex);
next(exchange);
return;
Expand All @@ -187,7 +192,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
}
}
default -> {
response.setInError(HttpStatus.SC_INTERNAL_SERVER_ERROR, "unknown pipeline type");
response.setInError(HttpStatus.SC_UNPROCESSABLE_ENTITY, "unknown pipeline type");
LOGGER.error("error executing pipeline: unknown type {} for /{}/{}/_aggrs/{}", query.getType(), request.getDBName(), request.getCollectionName(), queryUri);
next(exchange);
return;
Expand Down

0 comments on commit 10165e0

Please sign in to comment.