From 539e3960e997bede930345073baad4b5043ca525 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Tue, 10 Dec 2024 23:47:28 +0530 Subject: [PATCH] Add utils method for handle system exit --- .../runtime/engine/ArgumentHandler.java | 8 ++----- .../graphql/runtime/engine/DataLoader.java | 9 +++----- .../stdlib/graphql/runtime/engine/Engine.java | 22 ++++--------------- .../stdlib/graphql/runtime/utils/Utils.java | 9 ++++++++ spotbugs-exclude.xml | 2 ++ 5 files changed, 20 insertions(+), 30 deletions(-) diff --git a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/ArgumentHandler.java b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/ArgumentHandler.java index 730bb7252..492eb6af6 100644 --- a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/ArgumentHandler.java +++ b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/ArgumentHandler.java @@ -63,6 +63,7 @@ import static io.ballerina.stdlib.graphql.runtime.engine.EngineUtils.isEnum; import static io.ballerina.stdlib.graphql.runtime.engine.EngineUtils.isIgnoreType; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.INTERNAL_NODE; +import static io.ballerina.stdlib.graphql.runtime.utils.Utils.handleBErrorAndExit; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.isContext; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.isField; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.isFileUpload; @@ -545,12 +546,7 @@ private void addConstraintValidationErrors(Environment environment, BArray error environment.getRuntime().callMethod(this.responseGenerator, ADD_CONSTRAINT_ERRORS_METHOD, null, arguments); } catch (BError bError) { - bError.printStackTrace(); - // Service level `panic` is captured in this method. - // Since, `panic` is due to a critical application bug or resource exhaustion we need to exit the - // application. - // Please refer: https://github.com/ballerina-platform/ballerina-standard-library/issues/2714 - System.exit(1); + handleBErrorAndExit(bError); } return null; }); diff --git a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/DataLoader.java b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/DataLoader.java index d8449fe8c..44dcd9cd9 100644 --- a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/DataLoader.java +++ b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/DataLoader.java @@ -23,6 +23,8 @@ import io.ballerina.runtime.api.values.BObject; import io.ballerina.runtime.api.values.BTypedesc; +import static io.ballerina.stdlib.graphql.runtime.utils.Utils.handleBErrorAndExit; + /** * This class provides native implementations of the Ballerina DataLoader class. */ @@ -38,12 +40,7 @@ public static Object get(Environment env, BObject dataLoader, Object key, BTyped try { return env.getRuntime().callMethod(dataLoader, DATA_LOADER_PROCESSES_GET_METHOD_NAME, null, paramFeed); } catch (BError bError) { - bError.printStackTrace(); - // Service level `panic` is captured in this method. - // Since, `panic` is due to a critical application bug or resource exhaustion we need to exit the - // application. - // Please refer: https://github.com/ballerina-platform/ballerina-standard-library/issues/2714 - System.exit(1); + handleBErrorAndExit(bError); } return null; }); diff --git a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/Engine.java b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/Engine.java index 91825245c..b3684ac88 100644 --- a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/Engine.java +++ b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/engine/Engine.java @@ -66,6 +66,7 @@ import static io.ballerina.stdlib.graphql.runtime.utils.Utils.ERROR_TYPE; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.INTERNAL_NODE; import static io.ballerina.stdlib.graphql.runtime.utils.Utils.createError; +import static io.ballerina.stdlib.graphql.runtime.utils.Utils.handleBErrorAndExit; /** * This handles Ballerina GraphQL Engine. @@ -135,12 +136,7 @@ private static Object getResultObject(Environment environment, BObject context, try { return callResourceMethod(environment.getRuntime(), service, methodName, args); } catch (BError bError) { - bError.printStackTrace(); - // Service level `panic` is captured in this method. - // Since, `panic` is due to a critical application bug or resource exhaustion we need to exit the - // application. - // Please refer: https://github.com/ballerina-platform/ballerina-standard-library/issues/2714 - System.exit(1); + handleBErrorAndExit(bError); } return null; }); @@ -187,12 +183,7 @@ public static Object executeInterceptor(Environment environment, BObject interce try { return callResourceMethod(environment.getRuntime(), interceptor, remoteMethod.getName(), arguments); } catch (BError bError) { - bError.printStackTrace(); - // Service level `panic` is captured in this method. - // Since, `panic` is due to a critical application bug or resource exhaustion we need to exit the - // application. - // Please refer: https://github.com/ballerina-platform/ballerina-standard-library/issues/2714 - System.exit(1); + handleBErrorAndExit(bError); } return null; }); @@ -289,12 +280,7 @@ public static void executePrefetchMethod(Environment environment, BObject contex try { return callResourceMethod(environment.getRuntime(), service, resourceMethod.getName(), arguments); } catch (BError bError) { - bError.printStackTrace(); - // Service level `panic` is captured in this method. - // Since, `panic` is due to a critical application bug or resource exhaustion we need to exit the - // application. - // Please refer: https://github.com/ballerina-platform/ballerina-standard-library/issues/2714 - System.exit(1); + handleBErrorAndExit(bError); } return null; }); diff --git a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/utils/Utils.java b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/utils/Utils.java index fd368186d..c080c8a43 100644 --- a/native/src/main/java/io/ballerina/stdlib/graphql/runtime/utils/Utils.java +++ b/native/src/main/java/io/ballerina/stdlib/graphql/runtime/utils/Utils.java @@ -96,4 +96,13 @@ private static boolean hasExpectedModuleName(Type type, String expectedModuleNam public static BString getHashCode(BObject object) { return StringUtils.fromString(Integer.toString(object.hashCode())); } + + public static void handleBErrorAndExit(BError bError) { + bError.printStackTrace(); + // Service level `panic` is captured in this method. + // Since, `panic` is due to a critical application bug or resource exhaustion we need to exit the + // application. + // Please refer: https://github.com/ballerina-platform/ballerina-standard-library/issues/2714 + System.exit(1); + } } diff --git a/spotbugs-exclude.xml b/spotbugs-exclude.xml index a1cdb9c35..b04f5a9de 100644 --- a/spotbugs-exclude.xml +++ b/spotbugs-exclude.xml @@ -48,6 +48,8 @@ + +