From 439cb7076ba837a8b0309d03dc007f83a9d248af Mon Sep 17 00:00:00 2001 From: ADITYA-CODE-SOURCE Date: Sat, 14 Mar 2026 01:04:09 +0530 Subject: [PATCH] feat(licensedb): Add exception handling classes for error management Added custom exception classes for LicenseDB integration error handling: - LicenseDBException: Base exception class for all LicenseDB errors - LicenseDBConnectionException: Thrown when LicenseDB server is unreachable - LicenseDBAuthenticationException: Thrown when OAuth2 authentication fails - LicenseDBDataException: Thrown when data validation fails These exceptions provide structured error handling for various failure scenarios when communicating with the LicenseDB service. Fixes #3893 --- .../LicenseDBAuthenticationException.java | 59 +++++++++++++ .../LicenseDBConnectionException.java | 58 +++++++++++++ .../exception/LicenseDBDataException.java | 58 +++++++++++++ .../exception/LicenseDBException.java | 84 +++++++++++++++++++ 4 files changed, 259 insertions(+) create mode 100644 rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBAuthenticationException.java create mode 100644 rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBConnectionException.java create mode 100644 rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBDataException.java create mode 100644 rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBException.java diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBAuthenticationException.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBAuthenticationException.java new file mode 100644 index 0000000000..edaa770e12 --- /dev/null +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBAuthenticationException.java @@ -0,0 +1,59 @@ +/* + * Copyright Siemens AG, 2025. Part of the SW360 Portal Project. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.eclipse.sw360.rest.resourceserver.licensedb.exception; + +/** + * Exception thrown when OAuth2 authentication with LicenseDB fails. + * + *

This exception is thrown when the SW360 application cannot authenticate + * with the LicenseDB service using OAuth2 Machine-to-Machine credentials. + * This could be due to:

+ * + * + *

Example usage:

+ *
+ * try {
+ *     accessToken = oauth2Service.getAccessToken();
+ * } catch (LicenseDBAuthenticationException e) {
+ *     log.error("OAuth2 authentication failed: {}", e.getMessage());
+ *     // Handle authentication error
+ * }
+ * 
+ */ +public class LicenseDBAuthenticationException extends LicenseDBException { + + private static final long serialVersionUID = 1L; + + private static final String ERROR_CODE = "AUTHENTICATION_ERROR"; + + /** + * Constructs a new LicenseDBAuthenticationException with the specified message. + * + * @param message the error message describing the authentication failure + */ + public LicenseDBAuthenticationException(String message) { + super(message, ERROR_CODE); + } + + /** + * Constructs a new LicenseDBAuthenticationException with the specified message and cause. + * + * @param message the error message describing the authentication failure + * @param cause the underlying cause of the authentication failure + */ + public LicenseDBAuthenticationException(String message, Throwable cause) { + super(message, ERROR_CODE, cause); + } +} \ No newline at end of file diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBConnectionException.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBConnectionException.java new file mode 100644 index 0000000000..b243daa797 --- /dev/null +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBConnectionException.java @@ -0,0 +1,58 @@ +/* + * Copyright Siemens AG, 2025. Part of the SW360 Portal Project. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.eclipse.sw360.rest.resourceserver.licensedb.exception; + +/** + * Exception thrown when connection to LicenseDB server fails. + * + *

This exception is thrown when the SW360 application cannot establish + * a connection to the LicenseDB service. This could be due to:

+ * + * + *

Example usage:

+ *
+ * try {
+ *     licenses = licenseDBClient.getAllLicenses();
+ * } catch (LicenseDBConnectionException e) {
+ *     log.error("Failed to connect to LicenseDB: {}", e.getMessage());
+ *     // Handle connection error
+ * }
+ * 
+ */ +public class LicenseDBConnectionException extends LicenseDBException { + + private static final long serialVersionUID = 1L; + + private static final String ERROR_CODE = "CONNECTION_ERROR"; + + /** + * Constructs a new LicenseDBConnectionException with the specified message. + * + * @param message the error message describing the connection failure + */ + public LicenseDBConnectionException(String message) { + super(message, ERROR_CODE); + } + + /** + * Constructs a new LicenseDBConnectionException with the specified message and cause. + * + * @param message the error message describing the connection failure + * @param cause the underlying cause of the connection failure + */ + public LicenseDBConnectionException(String message, Throwable cause) { + super(message, ERROR_CODE, cause); + } +} \ No newline at end of file diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBDataException.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBDataException.java new file mode 100644 index 0000000000..339a91c237 --- /dev/null +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBDataException.java @@ -0,0 +1,58 @@ +/* + * Copyright Siemens AG, 2025. Part of the SW360 Portal Project. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.eclipse.sw360.rest.resourceserver.licensedb.exception; + +/** + * Exception thrown when LicenseDB data validation fails. + * + *

This exception is thrown when the data received from LicenseDB + * does not match the expected format or fails validation. This could be due to:

+ * + * + *

Example usage:

+ *
+ * try {
+ *     License license = transformer.transform(licenseDbData);
+ * } catch (LicenseDBDataException e) {
+ *     log.error("Invalid license data: {}", e.getMessage());
+ *     // Handle data validation error
+ * }
+ * 
+ */ +public class LicenseDBDataException extends LicenseDBException { + + private static final long serialVersionUID = 1L; + + private static final String ERROR_CODE = "DATA_ERROR"; + + /** + * Constructs a new LicenseDBDataException with the specified message. + * + * @param message the error message describing the data validation failure + */ + public LicenseDBDataException(String message) { + super(message, ERROR_CODE); + } + + /** + * Constructs a new LicenseDBDataException with the specified message and cause. + * + * @param message the error message describing the data validation failure + * @param cause the underlying cause of the data validation failure + */ + public LicenseDBDataException(String message, Throwable cause) { + super(message, ERROR_CODE, cause); + } +} \ No newline at end of file diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBException.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBException.java new file mode 100644 index 0000000000..916483ace3 --- /dev/null +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/licensedb/exception/LicenseDBException.java @@ -0,0 +1,84 @@ +/* + * Copyright Siemens AG, 2025. Part of the SW360 Portal Project. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.eclipse.sw360.rest.resourceserver.licensedb.exception; + +/** + * Base exception for all LicenseDB-related errors. + * + *

This exception serves as the parent class for all LicenseDB-specific + * exceptions in the SW360 application. It provides a common structure for + * error handling when interacting with the LicenseDB service.

+ * + *

Subclasses include:

+ * + */ +public class LicenseDBException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + private final String errorCode; + + /** + * Constructs a new LicenseDBException with the specified message. + * + * @param message the error message + */ + public LicenseDBException(String message) { + super(message); + this.errorCode = "UNKNOWN_ERROR"; + } + + /** + * Constructs a new LicenseDBException with the specified message and error code. + * + * @param message the error message + * @param errorCode the error code identifying the type of error + */ + public LicenseDBException(String message, String errorCode) { + super(message); + this.errorCode = errorCode; + } + + /** + * Constructs a new LicenseDBException with the specified message and cause. + * + * @param message the error message + * @param cause the cause of the exception + */ + public LicenseDBException(String message, Throwable cause) { + super(message, cause); + this.errorCode = "UNKNOWN_ERROR"; + } + + /** + * Constructs a new LicenseDBException with the specified message, error code, and cause. + * + * @param message the error message + * @param errorCode the error code identifying the type of error + * @param cause the cause of the exception + */ + public LicenseDBException(String message, String errorCode, Throwable cause) { + super(message, cause); + this.errorCode = errorCode; + } + + /** + * Returns the error code associated with this exception. + * + * @return the error code + */ + public String getErrorCode() { + return errorCode; + } +} \ No newline at end of file