From b362a9959a2d0f36d39c93a574bba8c26068407a Mon Sep 17 00:00:00 2001 From: Chamil Elladeniya Date: Tue, 23 Aug 2022 15:42:15 +0530 Subject: [PATCH 1/4] Exit the application when a panic occurs --- ballerina/Ballerina.toml | 4 ++-- .../ballerina/stdlib/http/api/HttpCallableUnitCallback.java | 5 +++++ .../java/io/ballerina/stdlib/http/api/HttpConstants.java | 1 + .../http/api/HttpResponseInterceptorUnitCallback.java | 6 ++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ballerina/Ballerina.toml b/ballerina/Ballerina.toml index 54b53bfb10..e2af96187e 100644 --- a/ballerina/Ballerina.toml +++ b/ballerina/Ballerina.toml @@ -19,13 +19,13 @@ path = "../native/build/libs/http-native-2.4.0-SNAPSHOT.jar" groupId = "io.ballerina.stdlib" artifactId = "mime-native" version = "2.4.0" -path = "./lib/mime-native-2.4.0-20220809-123900-18618a5.jar" +path = "./lib/mime-native-2.4.0-20220819-101600-3fd7442.jar" [[platform.java11.dependency]] groupId = "io.ballerina.stdlib" artifactId = "constraint-native" version = "1.0.0" -path = "./lib/constraint-native-1.0.0-20220808-183400-77a1dc1.jar" +path = "./lib/constraint-native-1.0.0-20220818-174100-7dd186d.jar" [[platform.java11.dependency]] path = "./lib/netty-common-4.1.77.Final.jar" diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpCallableUnitCallback.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpCallableUnitCallback.java index 4501503d5b..21cefd655d 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpCallableUnitCallback.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpCallableUnitCallback.java @@ -123,6 +123,10 @@ private void invokeBalMethod(Object[] paramFeed, String methodName) { public void notifySuccess(Object result) { stopObserverContext(); printStacktraceIfError(result); + Object isPanic = requestMessage.getProperty(HttpConstants.INTERCEPTOR_SERVICE_PANIC_ERROR); + if (Objects.nonNull(isPanic) && (boolean) isPanic) { + System.exit(0); + } } @Override @@ -157,6 +161,7 @@ public void notifyFailure(BError error) { // handles panic and check_panic if (alreadyResponded(error)) { return; } + requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_PANIC_ERROR, true); invokeErrorInterceptors(error, true); } diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java index e52da18020..36acfbaa6d 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpConstants.java @@ -369,6 +369,7 @@ public class HttpConstants { public static final String REQUEST_INTERCEPTOR_INDEX = "REQUEST_INTERCEPTOR_INDEX"; public static final String RESPONSE_INTERCEPTOR_INDEX = "RESPONSE_INTERCEPTOR_INDEX"; public static final String INTERCEPTOR_SERVICE_ERROR = "INTERCEPTOR_SERVICE_ERROR"; + public static final String INTERCEPTOR_SERVICE_PANIC_ERROR = "INTERCEPTOR_SERVICE_PANIC_ERROR"; public static final String WAIT_FOR_FULL_REQUEST = "WAIT_FOR_FULL_REQUEST"; public static final String HTTP_NORMAL = "Normal"; public static final String REQUEST_INTERCEPTOR = "RequestInterceptor"; diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpResponseInterceptorUnitCallback.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpResponseInterceptorUnitCallback.java index ca9b7b9228..01990ac154 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpResponseInterceptorUnitCallback.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpResponseInterceptorUnitCallback.java @@ -30,6 +30,8 @@ import io.ballerina.stdlib.http.api.nativeimpl.connection.Respond; import io.ballerina.stdlib.http.transport.message.HttpCarbonMessage; +import java.util.Objects; + /** * {@code HttpResponseInterceptorUnitCallback} is the responsible for acting on notifications received from Ballerina * side when a response interceptor service is invoked. @@ -181,6 +183,10 @@ private void invokeBalMethod(Object[] paramFeed, String methodName) { @Override public void notifySuccess(Object result) { printStacktraceIfError(result); + Object isPanic = requestMessage.getProperty(HttpConstants.INTERCEPTOR_SERVICE_PANIC_ERROR); + if (Objects.nonNull(isPanic) && (boolean) isPanic) { + System.exit(0); + } } @Override From 65d4561ada00832a254ede14b523d05eee885440 Mon Sep 17 00:00:00 2001 From: Chamil Elladeniya Date: Wed, 31 Aug 2022 09:58:02 +0530 Subject: [PATCH 2/4] Skip spotbugs warning due to system exit --- native/spotbugs-exclude.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/native/spotbugs-exclude.xml b/native/spotbugs-exclude.xml index 3b5160ea4c..eb6abd4fb2 100644 --- a/native/spotbugs-exclude.xml +++ b/native/spotbugs-exclude.xml @@ -49,6 +49,14 @@ + + + + + + + + From 7b97a796d1fa1ac90bff386f62890eadfe0c6aa8 Mon Sep 17 00:00:00 2001 From: Chamil Elladeniya Date: Wed, 31 Aug 2022 10:39:18 +0530 Subject: [PATCH 3/4] Update doc --- ballerina-tests/Dependencies.toml | 79 ------------------------------- changelog.md | 1 + docs/spec/spec.md | 7 +++ 3 files changed, 8 insertions(+), 79 deletions(-) diff --git a/ballerina-tests/Dependencies.toml b/ballerina-tests/Dependencies.toml index aa1e1136b7..f5dbb09d81 100644 --- a/ballerina-tests/Dependencies.toml +++ b/ballerina-tests/Dependencies.toml @@ -19,9 +19,6 @@ dependencies = [ {org = "ballerina", name = "log"}, {org = "ballerina", name = "regex"} ] -modules = [ - {org = "ballerina", packageName = "auth", moduleName = "auth"} -] [[package]] org = "ballerina" @@ -42,9 +39,6 @@ scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -modules = [ - {org = "ballerina", packageName = "constraint", moduleName = "constraint"} -] [[package]] org = "ballerina" @@ -55,9 +49,6 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"}, {org = "ballerina", name = "time"} ] -modules = [ - {org = "ballerina", packageName = "crypto", moduleName = "crypto"} -] [[package]] org = "ballerina" @@ -72,9 +63,6 @@ dependencies = [ {org = "ballerina", name = "regex"}, {org = "ballerina", name = "time"} ] -modules = [ - {org = "ballerina", packageName = "file", moduleName = "file"} -] [[package]] org = "ballerina" @@ -113,22 +101,9 @@ org = "ballerina" name = "http_tests" version = "2.4.0" dependencies = [ - {org = "ballerina", name = "auth"}, - {org = "ballerina", name = "constraint"}, - {org = "ballerina", name = "crypto"}, - {org = "ballerina", name = "file"}, {org = "ballerina", name = "http"}, - {org = "ballerina", name = "io"}, - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "jwt"}, - {org = "ballerina", name = "lang.boolean"}, - {org = "ballerina", name = "lang.float"}, - {org = "ballerina", name = "lang.int"}, - {org = "ballerina", name = "lang.runtime"}, {org = "ballerina", name = "lang.string"}, - {org = "ballerina", name = "lang.xml"}, {org = "ballerina", name = "mime"}, - {org = "ballerina", name = "oauth2"}, {org = "ballerina", name = "regex"}, {org = "ballerina", name = "test"}, {org = "ballerina", name = "url"} @@ -146,18 +121,12 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"}, {org = "ballerina", name = "lang.value"} ] -modules = [ - {org = "ballerina", packageName = "io", moduleName = "io"} -] [[package]] org = "ballerina" name = "jballerina.java" version = "0.0.0" scope = "testOnly" -modules = [ - {org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"} -] [[package]] org = "ballerina" @@ -174,9 +143,6 @@ dependencies = [ {org = "ballerina", name = "regex"}, {org = "ballerina", name = "time"} ] -modules = [ - {org = "ballerina", packageName = "jwt", moduleName = "jwt"} -] [[package]] org = "ballerina" @@ -198,18 +164,6 @@ dependencies = [ {org = "ballerina", name = "lang.__internal"} ] -[[package]] -org = "ballerina" -name = "lang.boolean" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"} -] -modules = [ - {org = "ballerina", packageName = "lang.boolean", moduleName = "lang.boolean"} -] - [[package]] org = "ballerina" name = "lang.decimal" @@ -219,18 +173,6 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.float" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"} -] -modules = [ - {org = "ballerina", packageName = "lang.float", moduleName = "lang.float"} -] - [[package]] org = "ballerina" name = "lang.int" @@ -239,9 +181,6 @@ scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -modules = [ - {org = "ballerina", packageName = "lang.int", moduleName = "lang.int"} -] [[package]] org = "ballerina" @@ -257,9 +196,6 @@ scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -modules = [ - {org = "ballerina", packageName = "lang.runtime", moduleName = "lang.runtime"} -] [[package]] org = "ballerina" @@ -282,18 +218,6 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.xml" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"} -] -modules = [ - {org = "ballerina", packageName = "lang.xml", moduleName = "lang.xml"} -] - [[package]] org = "ballerina" name = "log" @@ -332,9 +256,6 @@ dependencies = [ {org = "ballerina", name = "log"}, {org = "ballerina", name = "time"} ] -modules = [ - {org = "ballerina", packageName = "oauth2", moduleName = "oauth2"} -] [[package]] org = "ballerina" diff --git a/changelog.md b/changelog.md index 8dc0d2c3dd..e7c498e571 100644 --- a/changelog.md +++ b/changelog.md @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - [Make http2 the default transport in http](https://github.com/ballerina-platform/ballerina-standard-library/issues/454) - [Add support for client resource methods in HTTP client](https://github.com/ballerina-platform/ballerina-standard-library/issues/3102) - [Add constraint validation to HTTP payload binding](https://github.com/ballerina-platform/ballerina-standard-library/issues/3108) +- [Kill the application when a resource function panics](https://github.com/ballerina-platform/ballerina-standard-library/issues/2714) ### Changed - [Update default response status as HTTP 201 for POST resources](https://github.com/ballerina-platform/ballerina-standard-library/issues/2469) diff --git a/docs/spec/spec.md b/docs/spec/spec.md index e6fc225c05..3b1255a5c0 100644 --- a/docs/spec/spec.md +++ b/docs/spec/spec.md @@ -103,6 +103,7 @@ The conforming implementation of the specification is released and included in t * 8.2.2. [Error types](#822-error-types) * 8.2.3. [Trace log](#823-trace-log) * 8.2.4. [Access log](#824-access-log) + * 8.2.5. [Panic inside resource](#825-panic-inside-resource) 9. [Security](#9-security) * 9.1. [Authentication and Authorization](#91-authentication-and-authorization) * 9.1.1. [Declarative Approach](#911-declarative-approach) @@ -2327,6 +2328,12 @@ console = true # Default is false path = "testAccessLog.txt" # Optional ``` +#### 8.2.5 Panic inside resource + +Ballering consider panic as a catastrophic error and non recoverable. Hence immediate application termination is +performed to fail fast after responding to the request. This behaviour will be more useful in cloud environments as +well. + ## 9. Security ### 9.1 Authentication and Authorization From 7346641105d8ba35ade863e9f37231a304c0da80 Mon Sep 17 00:00:00 2001 From: Chamil Elladeniya Date: Thu, 1 Sep 2022 22:14:40 +0530 Subject: [PATCH 4/4] Fix auth desugar panic path --- ballerina-tests/Dependencies.toml | 79 +++++++++++++++++++ .../interceptors_error_handling_tests.bal | 14 ++-- ballerina/auth_desugar.bal | 6 +- ballerina/http_errors.bal | 6 ++ .../http/api/HttpCallableUnitCallback.java | 9 ++- .../stdlib/http/api/HttpErrorType.java | 4 +- .../HttpResponseInterceptorUnitCallback.java | 33 +------- 7 files changed, 105 insertions(+), 46 deletions(-) diff --git a/ballerina-tests/Dependencies.toml b/ballerina-tests/Dependencies.toml index f5dbb09d81..aa1e1136b7 100644 --- a/ballerina-tests/Dependencies.toml +++ b/ballerina-tests/Dependencies.toml @@ -19,6 +19,9 @@ dependencies = [ {org = "ballerina", name = "log"}, {org = "ballerina", name = "regex"} ] +modules = [ + {org = "ballerina", packageName = "auth", moduleName = "auth"} +] [[package]] org = "ballerina" @@ -39,6 +42,9 @@ scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +modules = [ + {org = "ballerina", packageName = "constraint", moduleName = "constraint"} +] [[package]] org = "ballerina" @@ -49,6 +55,9 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"}, {org = "ballerina", name = "time"} ] +modules = [ + {org = "ballerina", packageName = "crypto", moduleName = "crypto"} +] [[package]] org = "ballerina" @@ -63,6 +72,9 @@ dependencies = [ {org = "ballerina", name = "regex"}, {org = "ballerina", name = "time"} ] +modules = [ + {org = "ballerina", packageName = "file", moduleName = "file"} +] [[package]] org = "ballerina" @@ -101,9 +113,22 @@ org = "ballerina" name = "http_tests" version = "2.4.0" dependencies = [ + {org = "ballerina", name = "auth"}, + {org = "ballerina", name = "constraint"}, + {org = "ballerina", name = "crypto"}, + {org = "ballerina", name = "file"}, {org = "ballerina", name = "http"}, + {org = "ballerina", name = "io"}, + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "jwt"}, + {org = "ballerina", name = "lang.boolean"}, + {org = "ballerina", name = "lang.float"}, + {org = "ballerina", name = "lang.int"}, + {org = "ballerina", name = "lang.runtime"}, {org = "ballerina", name = "lang.string"}, + {org = "ballerina", name = "lang.xml"}, {org = "ballerina", name = "mime"}, + {org = "ballerina", name = "oauth2"}, {org = "ballerina", name = "regex"}, {org = "ballerina", name = "test"}, {org = "ballerina", name = "url"} @@ -121,12 +146,18 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"}, {org = "ballerina", name = "lang.value"} ] +modules = [ + {org = "ballerina", packageName = "io", moduleName = "io"} +] [[package]] org = "ballerina" name = "jballerina.java" version = "0.0.0" scope = "testOnly" +modules = [ + {org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"} +] [[package]] org = "ballerina" @@ -143,6 +174,9 @@ dependencies = [ {org = "ballerina", name = "regex"}, {org = "ballerina", name = "time"} ] +modules = [ + {org = "ballerina", packageName = "jwt", moduleName = "jwt"} +] [[package]] org = "ballerina" @@ -164,6 +198,18 @@ dependencies = [ {org = "ballerina", name = "lang.__internal"} ] +[[package]] +org = "ballerina" +name = "lang.boolean" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"} +] +modules = [ + {org = "ballerina", packageName = "lang.boolean", moduleName = "lang.boolean"} +] + [[package]] org = "ballerina" name = "lang.decimal" @@ -173,6 +219,18 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.float" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"} +] +modules = [ + {org = "ballerina", packageName = "lang.float", moduleName = "lang.float"} +] + [[package]] org = "ballerina" name = "lang.int" @@ -181,6 +239,9 @@ scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +modules = [ + {org = "ballerina", packageName = "lang.int", moduleName = "lang.int"} +] [[package]] org = "ballerina" @@ -196,6 +257,9 @@ scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +modules = [ + {org = "ballerina", packageName = "lang.runtime", moduleName = "lang.runtime"} +] [[package]] org = "ballerina" @@ -218,6 +282,18 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.xml" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"} +] +modules = [ + {org = "ballerina", packageName = "lang.xml", moduleName = "lang.xml"} +] + [[package]] org = "ballerina" name = "log" @@ -256,6 +332,9 @@ dependencies = [ {org = "ballerina", name = "log"}, {org = "ballerina", name = "time"} ] +modules = [ + {org = "ballerina", packageName = "oauth2", moduleName = "oauth2"} +] [[package]] org = "ballerina" diff --git a/ballerina-tests/tests/interceptors_error_handling_tests.bal b/ballerina-tests/tests/interceptors_error_handling_tests.bal index f7ff276f64..5b62a4cfb0 100644 --- a/ballerina-tests/tests/interceptors_error_handling_tests.bal +++ b/ballerina-tests/tests/interceptors_error_handling_tests.bal @@ -19,10 +19,10 @@ import ballerina/test; final http:Client noServiceRegisteredClientEP = check new("http://localhost:" + noServiceRegisteredTestPort.toString(), httpVersion = http:HTTP_1_1); -listener http:Listener noServiceRegisteredServerEP = new(noServiceRegisteredTestPort, +listener http:Listener noServiceRegisteredServerEP = new(noServiceRegisteredTestPort, httpVersion = http:HTTP_1_1, interceptors = [ - new LastResponseInterceptor(), new DefaultResponseErrorInterceptor(), new DefaultRequestInterceptor(), + new LastResponseInterceptor(), new DefaultResponseErrorInterceptor(), new DefaultRequestInterceptor(), new DefaultRequestErrorInterceptor(), new LastRequestInterceptor(), new DefaultResponseInterceptor() ] ); @@ -40,10 +40,10 @@ function testNoServiceRegistered() returns error? { final http:Client serviceErrorHandlingClientEP = check new("http://localhost:" + serviceErrorHandlingTestPort.toString(), httpVersion = http:HTTP_1_1); -listener http:Listener serviceErrorHandlingServerEP = new(serviceErrorHandlingTestPort, +listener http:Listener serviceErrorHandlingServerEP = new(serviceErrorHandlingTestPort, httpVersion = http:HTTP_1_1, interceptors = [ - new LastResponseInterceptor(), new DefaultResponseErrorInterceptor(), new DefaultRequestInterceptor(), + new LastResponseInterceptor(), new DefaultResponseErrorInterceptor(), new DefaultRequestInterceptor(), new DefaultRequestErrorInterceptor(), new LastRequestInterceptor(), new DefaultResponseInterceptor() ] ); @@ -233,7 +233,7 @@ function testConsumesProducesError() returns error? { assertHeaderValue(check res.getHeader("error-type"), "DispatchingError-Resource"); } -listener http:Listener authErrorHandlingServerEP = new(authErrorHandlingTestPort, +listener http:Listener authErrorHandlingServerEP = new(authErrorHandlingTestPort, httpVersion = http:HTTP_1_1, interceptors = [ new LastResponseInterceptor(), new DefaultResponseErrorInterceptor(), new DefaultRequestInterceptor(), @@ -282,7 +282,7 @@ function testAuthnError() returns error? { assertHeaderValue(check res.getHeader("last-interceptor"), "default-response-error-interceptor"); assertHeaderValue(check res.getHeader("default-response-error-interceptor"), "true"); assertHeaderValue(check res.getHeader("last-response-interceptor"), "true"); - assertHeaderValue(check res.getHeader("error-type"), "ListenerAuthenticationError"); + assertHeaderValue(check res.getHeader("error-type"), "ListenerAuthorizationError"); clientEP = check new("https://localhost:" + authErrorHandlingTestPort.toString(), secureSocket = { @@ -297,7 +297,7 @@ function testAuthnError() returns error? { assertHeaderValue(check res.getHeader("last-interceptor"), "default-response-error-interceptor"); assertHeaderValue(check res.getHeader("default-response-error-interceptor"), "true"); assertHeaderValue(check res.getHeader("last-response-interceptor"), "true"); - assertHeaderValue(check res.getHeader("error-type"), "ListenerAuthenticationError"); + assertHeaderValue(check res.getHeader("error-type"), "ListenerAuthorizationError"); } @test:Config{} diff --git a/ballerina/auth_desugar.bal b/ballerina/auth_desugar.bal index 743acd9a47..0195c34340 100644 --- a/ballerina/auth_desugar.bal +++ b/ballerina/auth_desugar.bal @@ -42,12 +42,12 @@ public isolated function authenticateResource(Service serviceRef, string methodN if header is string { Unauthorized|Forbidden? result = tryAuthenticate(authConfig, header); if result is Unauthorized { - panic error ListenerAuthnError(""); + panic error InternalListenerAuthnError(""); } else if result is Forbidden { - panic error ListenerAuthzError(""); + panic error InternalListenerAuthzError(""); } } else { - panic error ListenerAuthnError(""); + panic error InternalListenerAuthnError(""); } } diff --git a/ballerina/http_errors.bal b/ballerina/http_errors.bal index 95af5d48f7..45aae4acd8 100644 --- a/ballerina/http_errors.bal +++ b/ballerina/http_errors.bal @@ -103,6 +103,12 @@ public type ListenerAuthnError distinct ListenerAuthError; # Defines the authorization error types that returned from listener. public type ListenerAuthzError distinct ListenerAuthError; +# Defined for internal use when panicing from the auth_desugar +type InternalListenerAuthnError distinct ListenerAuthError; + +# Defined for internal use when panicing from the auth_desugar +type InternalListenerAuthzError distinct ListenerAuthError; + # Defines the client error types that returned while sending outbound request. public type OutboundRequestError distinct ClientError; diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpCallableUnitCallback.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpCallableUnitCallback.java index 21cefd655d..b07611895f 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpCallableUnitCallback.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpCallableUnitCallback.java @@ -117,7 +117,7 @@ private void returnErrorResponse(BError error) { invokeBalMethod(paramFeed, "returnErrorResponse"); } - private void invokeBalMethod(Object[] paramFeed, String methodName) { + public void invokeBalMethod(Object[] paramFeed, String methodName) { Callback returnCallback = new Callback() { @Override public void notifySuccess(Object result) { @@ -153,15 +153,16 @@ private void stopObserverContext() { public void notifyFailure(BError error) { // handles panic and check_panic cleanupRequestMessage(); // This check is added to update the status code with respect to the auth errors. - if (error.getType().getName().equals(HttpErrorType.LISTENER_AUTHN_ERROR.getErrorName())) { + if (error.getType().getName().equals(HttpErrorType.INTERNAL_LISTENER_AUTHN_ERROR.getErrorName())) { requestMessage.setHttpStatusCode(401); - } else if (error.getType().getName().equals(HttpErrorType.LISTENER_AUTHZ_ERROR.getErrorName())) { + } else if (error.getType().getName().equals(HttpErrorType.INTERNAL_LISTENER_AUTHZ_ERROR.getErrorName())) { requestMessage.setHttpStatusCode(403); + } else { + requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_PANIC_ERROR, true); } if (alreadyResponded(error)) { return; } - requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_PANIC_ERROR, true); invokeErrorInterceptors(error, true); } diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpErrorType.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpErrorType.java index a8163db579..c886f77a2a 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpErrorType.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpErrorType.java @@ -72,8 +72,8 @@ public enum HttpErrorType { REQ_DISPATCHING_ERROR("RequestDispatchingError"), SERVICE_DISPATCHING_ERROR("ServiceDispatchingError"), RESOURCE_DISPATCHING_ERROR("ResourceDispatchingError"), - LISTENER_AUTHZ_ERROR("ListenerAuthzError"), - LISTENER_AUTHN_ERROR("ListenerAuthnError"); + INTERNAL_LISTENER_AUTHZ_ERROR("InternalListenerAuthzError"), + INTERNAL_LISTENER_AUTHN_ERROR("InternalListenerAuthnError"); private final String errorName; diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpResponseInterceptorUnitCallback.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpResponseInterceptorUnitCallback.java index 01990ac154..85d2ac9f4e 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpResponseInterceptorUnitCallback.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpResponseInterceptorUnitCallback.java @@ -19,24 +19,19 @@ package io.ballerina.stdlib.http.api; import io.ballerina.runtime.api.Environment; -import io.ballerina.runtime.api.PredefinedTypes; import io.ballerina.runtime.api.Runtime; -import io.ballerina.runtime.api.async.Callback; import io.ballerina.runtime.api.types.ServiceType; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BObject; -import io.ballerina.stdlib.http.api.nativeimpl.ModuleUtils; import io.ballerina.stdlib.http.api.nativeimpl.connection.Respond; import io.ballerina.stdlib.http.transport.message.HttpCarbonMessage; -import java.util.Objects; - /** * {@code HttpResponseInterceptorUnitCallback} is the responsible for acting on notifications received from Ballerina * side when a response interceptor service is invoked. */ -public class HttpResponseInterceptorUnitCallback implements Callback { +public class HttpResponseInterceptorUnitCallback extends HttpCallableUnitCallback { private static final String ILLEGAL_FUNCTION_INVOKED = "illegal return: response has already been sent"; private final HttpCarbonMessage requestMessage; @@ -45,18 +40,17 @@ public class HttpResponseInterceptorUnitCallback implements Callback { private final Environment environment; private final BObject requestCtx; private final DataContext dataContext; - private final Runtime runtime; public HttpResponseInterceptorUnitCallback(HttpCarbonMessage requestMessage, BObject caller, BObject response, Environment env, DataContext dataContext, Runtime runtime) { + super(requestMessage, runtime); this.requestMessage = requestMessage; this.requestCtx = (BObject) requestMessage.getProperty(HttpConstants.REQUEST_CONTEXT); this.caller = caller; this.response = response; this.environment = env; this.dataContext = dataContext; - this.runtime = runtime; } @Override @@ -74,7 +68,7 @@ public void notifyFailure(BError error) { // handles panic and check_panic invokeErrorInterceptors(error, false); } - private void invokeErrorInterceptors(BError error, boolean printError) { + public void invokeErrorInterceptors(BError error, boolean printError) { requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_ERROR, error); if (printError) { error.printStackTrace(); @@ -177,25 +171,4 @@ public void returnErrorResponse(BError error) { invokeBalMethod(paramFeed, "returnErrorResponse"); } - - private void invokeBalMethod(Object[] paramFeed, String methodName) { - Callback returnCallback = new Callback() { - @Override - public void notifySuccess(Object result) { - printStacktraceIfError(result); - Object isPanic = requestMessage.getProperty(HttpConstants.INTERCEPTOR_SERVICE_PANIC_ERROR); - if (Objects.nonNull(isPanic) && (boolean) isPanic) { - System.exit(0); - } - } - - @Override - public void notifyFailure(BError result) { - sendFailureResponse(result); - } - }; - runtime.invokeMethodAsyncSequentially( - caller, methodName, null, ModuleUtils.getNotifySuccessMetaData(), - returnCallback, null, PredefinedTypes.TYPE_NULL, paramFeed); - } }