Skip to content

Commit

Permalink
Return internal errors from native code
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Jan 29, 2024
1 parent 39fb374 commit 254b865
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
import static io.ballerina.stdlib.http.api.HttpConstants.QUERY_STRING_SEPARATOR;
import static io.ballerina.stdlib.http.api.HttpConstants.REQUEST_CTX_MEMBERS;
import static io.ballerina.stdlib.http.api.HttpConstants.WHITESPACE;
import static io.ballerina.stdlib.http.api.HttpErrorType.SERVICE_NOT_FOUND_ERROR;
import static io.ballerina.stdlib.http.api.HttpErrorType.INTERNAL_SERVICE_NOT_FOUND_ERROR;
import static io.ballerina.stdlib.http.api.HttpUtil.getParameterTypes;

/**
Expand Down Expand Up @@ -90,7 +90,7 @@ public static HttpService findService(HTTPServicesRegistry servicesRegistry, Htt
} else {
String localAddress = inboundReqMsg.getProperty(HttpConstants.LOCAL_ADDRESS).toString();
String message = "no service has registered for listener : " + localAddress;
throw HttpUtil.createHttpStatusCodeError(SERVICE_NOT_FOUND_ERROR, message);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_SERVICE_NOT_FOUND_ERROR, message);
}

String rawUri = (String) inboundReqMsg.getProperty(HttpConstants.TO);
Expand All @@ -104,7 +104,7 @@ public static HttpService findService(HTTPServicesRegistry servicesRegistry, Htt

if (basePath == null) {
String message = "no matching service found for path";
throw HttpUtil.createHttpStatusCodeError(SERVICE_NOT_FOUND_ERROR, message);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_SERVICE_NOT_FOUND_ERROR, message);
}

HttpService service = servicesOnInterface.get(basePath);
Expand All @@ -117,7 +117,7 @@ public static HttpService findService(HTTPServicesRegistry servicesRegistry, Htt
return service;
} catch (Exception e) {
if (!(e instanceof BError)) {
throw HttpUtil.createHttpStatusCodeError(SERVICE_NOT_FOUND_ERROR, e.getMessage());
throw HttpUtil.createHttpStatusCodeError(INTERNAL_SERVICE_NOT_FOUND_ERROR, e.getMessage());
}
throw e;
}
Expand Down Expand Up @@ -154,7 +154,7 @@ public static InterceptorService findInterceptorService(HTTPInterceptorServicesR
} else {
String localAddress = inboundReqMsg.getProperty(HttpConstants.LOCAL_ADDRESS).toString();
String message = "no service has registered for listener : " + localAddress;
throw HttpUtil.createHttpStatusCodeError(SERVICE_NOT_FOUND_ERROR, message);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_SERVICE_NOT_FOUND_ERROR, message);
}

if (isResponsePath) {
Expand All @@ -178,15 +178,15 @@ public static InterceptorService findInterceptorService(HTTPInterceptorServicesR

if (basePath == null) {
String message = "no matching service found for path";
throw HttpUtil.createHttpStatusCodeError(SERVICE_NOT_FOUND_ERROR, message);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_SERVICE_NOT_FOUND_ERROR, message);
}

InterceptorService service = servicesOnInterface.get(basePath);
setInboundReqProperties(inboundReqMsg, rawPathAndQuery[0], basePath, rawPathAndQuery[1]);
return service;
} catch (Exception e) {
if (!(e instanceof BError)) {
throw HttpUtil.createHttpStatusCodeError(SERVICE_NOT_FOUND_ERROR, e.getMessage());
throw HttpUtil.createHttpStatusCodeError(INTERNAL_SERVICE_NOT_FOUND_ERROR, e.getMessage());
}
throw e;
}
Expand Down Expand Up @@ -231,14 +231,14 @@ public static HttpResource findResource(HTTPServicesRegistry servicesRegistry, H
String protocol = (String) inboundMessage.getProperty(HttpConstants.PROTOCOL);
if (protocol == null) {
throw HttpUtil.createHttpError("protocol not defined in the incoming request",
HttpErrorType.REQ_DISPATCHING_ERROR);
HttpErrorType.INTERNAL_REQ_DISPATCHING_ERROR);
}

// Find the Service TODO can be improved
HttpService service = HttpDispatcher.findService(servicesRegistry, inboundMessage, false);
if (service == null) {
throw HttpUtil.createHttpError("no Service found to handle the service request",
HttpErrorType.REQ_DISPATCHING_ERROR);
HttpErrorType.INTERNAL_REQ_DISPATCHING_ERROR);
// Finer details of the errors are thrown from the dispatcher itself, Ideally we shouldn't get here.
}

Expand All @@ -251,14 +251,14 @@ public static InterceptorResource findInterceptorResource(HTTPInterceptorService
String protocol = (String) inboundMessage.getProperty(HttpConstants.PROTOCOL);
if (protocol == null) {
throw HttpUtil.createHttpError("protocol not defined in the incoming request",
HttpErrorType.REQ_DISPATCHING_ERROR);
HttpErrorType.INTERNAL_REQ_DISPATCHING_ERROR);
}

// Find the Service TODO can be improved
InterceptorService service = HttpDispatcher.findInterceptorService(servicesRegistry, inboundMessage, false);
if (service == null) {
throw HttpUtil.createHttpError("no Service found to handle the service request",
HttpErrorType.REQ_DISPATCHING_ERROR);
HttpErrorType.INTERNAL_REQ_DISPATCHING_ERROR);
// Finer details of the errors are thrown from the dispatcher itself, Ideally we shouldn't get here.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,26 @@ public enum HttpErrorType {
INVALID_CONTENT_LENGTH("InvalidContentLengthError"),
HEADER_NOT_FOUND_ERROR("HeaderNotFoundError"),
CLIENT_ERROR("ClientError"),
PAYLOAD_BINDING_LISTENER_ERROR("PayloadBindingListenerError"),
PAYLOAD_VALIDATION_LISTENER_ERROR("PayloadValidationListenerError"),
HEADER_BINDING_ERROR("HeaderBindingError"),
QUERY_PARAM_BINDING_ERROR("QueryParameterBindingError"),
PATH_PARAM_BINDING_ERROR("PathParameterBindingError"),
INTERCEPTOR_RETURN_ERROR("InterceptorReturnError"),
REQ_DISPATCHING_ERROR("RequestDispatchingError"),
RESOURCE_NOT_FOUND_ERROR("ResourceNotFoundError"),
RESOURCE_METHOD_NOT_ALLOWED_ERROR("ResourceMethodNotAllowedError"),
UNSUPPORTED_REQUEST_MEDIA_TYPE_ERROR("UnsupportedRequestMediaTypeError"),
REQUEST_NOT_ACCEPTABLE_ERROR("RequestNotAcceptableError"),
SERVICE_NOT_FOUND_ERROR("ServiceNotFoundError"),
BAD_MATRIX_PARAMS_ERROR("BadMatrixParamError"),
RESOURCE_DISPATCHING_SERVER_ERROR("ResourceDispatchingServerError"),
INTERNAL_PAYLOAD_BINDING_LISTENER_ERROR("InternalPayloadBindingListenerError"),
INTERNAL_PAYLOAD_VALIDATION_LISTENER_ERROR("InternalPayloadValidationListenerError"),
INTERNAL_HEADER_BINDING_ERROR("InternalHeaderBindingError"),
INTERNAL_QUERY_PARAM_BINDING_ERROR("InternalQueryParameterBindingError"),
INTERNAL_PATH_PARAM_BINDING_ERROR("InternalPathParameterBindingError"),
INTERNAL_INTERCEPTOR_RETURN_ERROR("InternalInterceptorReturnError"),
INTERNAL_REQ_DISPATCHING_ERROR("InternalRequestDispatchingError"),
INTERNAL_RESOURCE_NOT_FOUND_ERROR("InternalResourceNotFoundError"),
INTERNAL_RESOURCE_METHOD_NOT_ALLOWED_ERROR("InternalResourceMethodNotAllowedError"),
INTERNAL_UNSUPPORTED_REQUEST_MEDIA_TYPE_ERROR("InternalUnsupportedRequestMediaTypeError"),
INTERNAL_REQUEST_NOT_ACCEPTABLE_ERROR("InternalRequestNotAcceptableError"),
INTERNAL_SERVICE_NOT_FOUND_ERROR("InternalServiceNotFoundError"),
INTERNAL_BAD_MATRIX_PARAMS_ERROR("InternalBadMatrixParamError"),
INTERNAL_RESOURCE_DISPATCHING_SERVER_ERROR("InternalResourceDispatchingServerError"),
INTERNAL_LISTENER_AUTHZ_ERROR("InternalListenerAuthzError"),
INTERNAL_LISTENER_AUTHN_ERROR("InternalListenerAuthnError"),
CLIENT_CONNECTOR_ERROR("ClientConnectorError"),
RESOURCE_PATH_VALIDATION_ERROR("ResourcePathValidationError"),
HEADER_VALIDATION_ERROR("HeaderValidationError"),
QUERY_PARAM_VALIDATION_ERROR("QueryParameterValidationError");
INTERNAL_RESOURCE_PATH_VALIDATION_ERROR("InternalResourcePathValidationError"),
INTERNAL_HEADER_VALIDATION_ERROR("InternalHeaderValidationError"),
INTERNAL_QUERY_PARAM_VALIDATION_ERROR("InternalQueryParameterValidationError");

private final String errorName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import java.util.stream.Collectors;

import static io.ballerina.stdlib.http.api.HttpErrorType.GENERIC_LISTENER_ERROR;
import static io.ballerina.stdlib.http.api.HttpErrorType.REQUEST_NOT_ACCEPTABLE_ERROR;
import static io.ballerina.stdlib.http.api.HttpErrorType.RESOURCE_METHOD_NOT_ALLOWED_ERROR;
import static io.ballerina.stdlib.http.api.HttpErrorType.UNSUPPORTED_REQUEST_MEDIA_TYPE_ERROR;
import static io.ballerina.stdlib.http.api.HttpErrorType.INTERNAL_REQUEST_NOT_ACCEPTABLE_ERROR;
import static io.ballerina.stdlib.http.api.HttpErrorType.INTERNAL_RESOURCE_METHOD_NOT_ALLOWED_ERROR;
import static io.ballerina.stdlib.http.api.HttpErrorType.INTERNAL_UNSUPPORTED_REQUEST_MEDIA_TYPE_ERROR;

/**
* Http Node Item for URI template tree.
Expand Down Expand Up @@ -137,7 +137,7 @@ private Resource validateHTTPMethod(List<Resource> resources, HttpCarbonMessage
}
if (!isOptionsRequest) {
String message = "Method not allowed";
throw HttpUtil.createHttpStatusCodeError(RESOURCE_METHOD_NOT_ALLOWED_ERROR, message);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_RESOURCE_METHOD_NOT_ALLOWED_ERROR, message);
}
return null;
}
Expand Down Expand Up @@ -189,7 +189,7 @@ private void validateConsumes(Resource resource, HttpCarbonMessage cMsg) {
}
}
String message = "content-type : " + contentMediaType + " is not supported";
throw HttpUtil.createHttpStatusCodeError(UNSUPPORTED_REQUEST_MEDIA_TYPE_ERROR, message);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_UNSUPPORTED_REQUEST_MEDIA_TYPE_ERROR, message);
}

private String extractContentMediaType(String header) {
Expand Down Expand Up @@ -231,7 +231,7 @@ private void validateProduces(Resource resource, HttpCarbonMessage cMsg) {
return;
}
}
throw HttpUtil.createHttpStatusCodeError(REQUEST_NOT_ACCEPTABLE_ERROR, "Request is not acceptable");
throw HttpUtil.createHttpStatusCodeError(INTERNAL_REQUEST_NOT_ACCEPTABLE_ERROR, "Request is not acceptable");
}

private List<String> extractAcceptMediaTypes(String header) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;

import static io.ballerina.stdlib.http.api.HttpErrorType.RESOURCE_DISPATCHING_SERVER_ERROR;
import static io.ballerina.stdlib.http.api.HttpErrorType.RESOURCE_NOT_FOUND_ERROR;
import static io.ballerina.stdlib.http.api.HttpErrorType.INTERNAL_RESOURCE_DISPATCHING_SERVER_ERROR;
import static io.ballerina.stdlib.http.api.HttpErrorType.INTERNAL_RESOURCE_NOT_FOUND_ERROR;

/**
* Resource level dispatchers handler for HTTP protocol.
Expand Down Expand Up @@ -56,12 +56,12 @@ public static Resource findResource(Service service, HttpCarbonMessage inboundRe
handleOptionsRequest(inboundRequest, service);
} else {
String message = "no matching resource found for path";
throw HttpUtil.createHttpStatusCodeError(RESOURCE_NOT_FOUND_ERROR, message);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_RESOURCE_NOT_FOUND_ERROR, message);
}
return null;
}
} catch (URITemplateException e) {
throw HttpUtil.createHttpStatusCodeError(RESOURCE_DISPATCHING_SERVER_ERROR, e.getMessage());
throw HttpUtil.createHttpStatusCodeError(INTERNAL_RESOURCE_DISPATCHING_SERVER_ERROR, e.getMessage());
}
}

Expand All @@ -86,7 +86,7 @@ private static void handleOptionsRequest(HttpCarbonMessage cMsg, Service service
DispatcherUtil.concatValues(service.getAllAllowedMethods(), false));
} else {
String message = "no matching resource found for path";
throw HttpUtil.createHttpStatusCodeError(RESOURCE_NOT_FOUND_ERROR, message);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_RESOURCE_NOT_FOUND_ERROR, message);
}
CorsHeaderGenerator.process(cMsg, response, false);
String introspectionResourcePathHeaderValue = service.getIntrospectionResourcePathHeaderValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import io.ballerina.stdlib.http.api.HttpUtil;
import org.ballerinalang.langlib.value.EnsureType;

import static io.ballerina.stdlib.http.api.HttpErrorType.INTERCEPTOR_RETURN_ERROR;
import static io.ballerina.stdlib.http.api.HttpErrorType.INTERNAL_INTERCEPTOR_RETURN_ERROR;

/**
* Utilities related to HTTP request context.
Expand Down Expand Up @@ -61,13 +61,14 @@ public static Object next(BObject requestCtx) {
if (!isInterceptorService(requestCtx)) {
// TODO : After introducing response interceptors, calling ctx.next() should return "illegal function
// invocation : next()" if there is a response interceptor service in the pipeline
return HttpUtil.createHttpStatusCodeError(INTERCEPTOR_RETURN_ERROR, "no next service to be returned");
return HttpUtil.createHttpStatusCodeError(INTERNAL_INTERCEPTOR_RETURN_ERROR,
"no next service to be returned");
}
requestCtx.addNativeData(HttpConstants.REQUEST_CONTEXT_NEXT, true);
return getNextInterceptor(requestCtx, interceptors);
} else {
String message = "request context object does not contain the configured interceptors";
return HttpUtil.createHttpStatusCodeError(INTERCEPTOR_RETURN_ERROR, message);
return HttpUtil.createHttpStatusCodeError(INTERNAL_INTERCEPTOR_RETURN_ERROR, message);
}
}

Expand All @@ -90,7 +91,8 @@ private static Object getNextInterceptor(BObject requestCtx, BArray interceptors
}
}
if (interceptorIndex > interceptors.size()) {
return HttpUtil.createHttpStatusCodeError(INTERCEPTOR_RETURN_ERROR, "no next service to be returned");
return HttpUtil.createHttpStatusCodeError(INTERNAL_INTERCEPTOR_RETURN_ERROR,
"no next service to be returned");
}
if (interceptorIndex < 0) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.ArrayList;
import java.util.List;

import static io.ballerina.stdlib.http.api.HttpErrorType.HEADER_BINDING_ERROR;
import static io.ballerina.stdlib.http.api.HttpErrorType.INTERNAL_HEADER_BINDING_ERROR;
import static io.ballerina.stdlib.http.api.service.signature.ParamUtils.castParam;
import static io.ballerina.stdlib.http.api.service.signature.ParamUtils.castParamArray;

Expand Down Expand Up @@ -79,7 +79,7 @@ public void populateFeed(HttpCarbonMessage httpCarbonMessage, Object[] paramFeed
castedHeader = ValueUtils.convert(parsedHeader, headerParam.getOriginalType());
} catch (Exception ex) {
String message = "header binding failed for parameter: '" + headerParam.getHeaderName() + "'";
throw HttpUtil.createHttpStatusCodeError(HEADER_BINDING_ERROR, message, null,
throw HttpUtil.createHttpStatusCodeError(INTERNAL_HEADER_BINDING_ERROR, message, null,
HttpUtil.createError(ex));
}
paramFeed[index++] = headerParam.validateConstraints(castedHeader);
Expand All @@ -95,7 +95,7 @@ public void populateFeed(HttpCarbonMessage httpCarbonMessage, Object[] paramFeed
continue;
} else {
String message = "no header value found for '" + token + "'";
throw HttpUtil.createHttpStatusCodeError(HEADER_BINDING_ERROR, message);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_HEADER_BINDING_ERROR, message);
}
}
if (headerValues.size() == 1 && headerValues.get(0).isEmpty()) {
Expand All @@ -105,7 +105,7 @@ public void populateFeed(HttpCarbonMessage httpCarbonMessage, Object[] paramFeed
continue;
} else {
String message = "no header value found for '" + token + "'";
throw HttpUtil.createHttpStatusCodeError(HEADER_BINDING_ERROR, message);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_HEADER_BINDING_ERROR, message);
}
}
int typeTag = headerParam.getEffectiveTypeTag();
Expand All @@ -120,7 +120,7 @@ public void populateFeed(HttpCarbonMessage httpCarbonMessage, Object[] paramFeed
castedHeaderValue = ValueUtils.convert(parsedHeaderValue, headerParam.getOriginalType());
} catch (Exception ex) {
String message = "header binding failed for parameter: '" + token + "'";
throw HttpUtil.createHttpStatusCodeError(HEADER_BINDING_ERROR, message, null,
throw HttpUtil.createHttpStatusCodeError(INTERNAL_HEADER_BINDING_ERROR, message, null,
HttpUtil.createError(ex));
}

Expand All @@ -147,7 +147,7 @@ private BMap<BString, Object> processHeaderRecord(HeaderParam headerParam, HttpH
return null;
} else {
String message = "no header value found for '" + key + "'";
throw HttpUtil.createHttpStatusCodeError(HEADER_BINDING_ERROR, message);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_HEADER_BINDING_ERROR, message);
}
}
if (headerValues.size() == 1 && headerValues.get(0).isEmpty()) {
Expand All @@ -158,7 +158,7 @@ private BMap<BString, Object> processHeaderRecord(HeaderParam headerParam, HttpH
return null;
} else {
String message = "no header value found for '" + key + "'";
throw HttpUtil.createHttpStatusCodeError(HEADER_BINDING_ERROR, message);
throw HttpUtil.createHttpStatusCodeError(INTERNAL_HEADER_BINDING_ERROR, message);
}
}
try {
Expand All @@ -171,7 +171,7 @@ private BMap<BString, Object> processHeaderRecord(HeaderParam headerParam, HttpH
}
} catch (Exception ex) {
String message = "header binding failed for parameter: '" + key + "'";
throw HttpUtil.createHttpStatusCodeError(HEADER_BINDING_ERROR, message, null,
throw HttpUtil.createHttpStatusCodeError(INTERNAL_HEADER_BINDING_ERROR, message, null,
HttpUtil.createError(ex));
}
}
Expand Down
Loading

0 comments on commit 254b865

Please sign in to comment.