From 1b3352e0739782abb4ff0b400dbbc68cd3711ce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20R=C3=B6gner?= Date: Mon, 14 Apr 2025 14:47:06 +0200 Subject: [PATCH] DS-1064: Improve exception handling in PLV8 based functions to contain stack traces & causes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Improve FeatureWriter to use new Exception constructor to pass error causes Signed-off-by: Benjamin Rögner --- xyz-util/src/main/resources/sql/Exception.js | 45 +++++++++++-------- .../src/main/resources/sql/FeatureWriter.js | 4 +- .../src/main/resources/sql/feature_writer.sql | 4 +- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/xyz-util/src/main/resources/sql/Exception.js b/xyz-util/src/main/resources/sql/Exception.js index b631ebc132..6059f96304 100644 --- a/xyz-util/src/main/resources/sql/Exception.js +++ b/xyz-util/src/main/resources/sql/Exception.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2024 HERE Europe B.V. + * Copyright (C) 2017-2025 HERE Europe B.V. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,10 +22,22 @@ class Exception extends Error { context; detail; hint; + cause; - constructor(message) { + constructor(message, cause = null) { super(message); this.withDetail(this.constructor.name + ": "); + this.cause = cause; + + //Build the context information + this.context = this.stack; + + if (cause instanceof Exception) + this.context += "\nCaused by: " + cause.context; + else if (cause instanceof Error) + this.context += "\nCaused by: " + cause.stack; + else + this.context += "\nCaused by: " + cause; } withCode(code) { @@ -40,11 +52,6 @@ class Exception extends Error { return this; } - withContext(context) { - this.context = context; - return this; - } - withDetail(detail) { this.detail = detail; return this; @@ -57,43 +64,43 @@ class Exception extends Error { } class XyzException extends Exception { - constructor(message) { - super(message); + constructor(message, cause = null) { + super(message, cause); this.withCode("XYZ50"); } } class VersionConflictError extends XyzException { - constructor(message) { - super(message); + constructor(message, cause = null) { + super(message, cause); this.withCode("XYZ49"); } } class MergeConflictError extends VersionConflictError { - constructor(message) { - super(message); + constructor(message, cause = null) { + super(message, cause); this.withCode("XYZ48"); } } class IllegalArgumentException extends XyzException { - constructor(message) { - super(message); + constructor(message, cause = null) { + super(message, cause); this.withCode("XYZ40"); } } class FeatureExistsException extends XyzException { - constructor(message) { - super(message); + constructor(message, cause = null) { + super(message, cause); this.withCode("XYZ20"); } } class FeatureNotExistsException extends XyzException { - constructor(message) { - super(message); + constructor(message, cause = null) { + super(message, cause); this.withCode("XYZ44"); } } diff --git a/xyz-util/src/main/resources/sql/FeatureWriter.js b/xyz-util/src/main/resources/sql/FeatureWriter.js index 0a0721cc43..8861304a27 100644 --- a/xyz-util/src/main/resources/sql/FeatureWriter.js +++ b/xyz-util/src/main/resources/sql/FeatureWriter.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2024 HERE Europe B.V. + * Copyright (C) 2017-2025 HERE Europe B.V. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -342,7 +342,7 @@ class FeatureWriter { if (this.onExists == "ERROR") this._throwFeatureExistsError(); else - throw new XyzException("Unexpected conflict while trying to perform write operation.").withDetail(e.detail).withHint(e.hint); + throw new XyzException("Unexpected conflict while trying to perform write operation.", e).withDetail(e.detail).withHint(e.hint); } this.debugBox(e.stack); diff --git a/xyz-util/src/main/resources/sql/feature_writer.sql b/xyz-util/src/main/resources/sql/feature_writer.sql index 206c1f5f56..364fc6ca2d 100644 --- a/xyz-util/src/main/resources/sql/feature_writer.sql +++ b/xyz-util/src/main/resources/sql/feature_writer.sql @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2024 HERE Europe B.V. + * Copyright (C) 2017-2025 HERE Europe B.V. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,7 +72,7 @@ $BODY$ } catch (error) { if (!error.code) - throw new Error("Unexpected error in feature_writer: " + error.message); + throw new Exception("Unexpected error in feature_writer: " + error.message, error); else throw error; }