-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved implementation of KapuaException and KapuaRuntimeException
Signed-off-by: coduz <alberto.codutti@eurotech.com>
- Loading branch information
Showing
7 changed files
with
246 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
service/api/src/main/java/org/eclipse/kapua/ExceptionMessageUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016, 2019 Eurotech and/or its affiliates and others | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Eurotech - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.kapua; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import java.text.MessageFormat; | ||
import java.util.Locale; | ||
import java.util.MissingResourceException; | ||
import java.util.ResourceBundle; | ||
import java.util.StringJoiner; | ||
|
||
/** | ||
* Utilities to print localized error messages for {@link KapuaException}s. | ||
* | ||
* @since 1.1.0 | ||
*/ | ||
public class ExceptionMessageUtils { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ExceptionMessageUtils.class); | ||
|
||
private static final String KAPUA_GENERIC_MESSAGE = "Error: {0}"; | ||
|
||
private ExceptionMessageUtils() { | ||
} | ||
|
||
/** | ||
* Gets the {@link Locale}ised message for the given {@link KapuaErrorCode}. | ||
* <p> | ||
* If the {@link ResourceBundle} or the {@link KapuaErrorCode} are not found, a generic message is printed. | ||
* | ||
* @param resourceBundleName The {@link ResourceBundle} name from which to search the {@link KapuaErrorCode}. | ||
* @param locale The {@link Locale} of the {@link ResourceBundle}. | ||
* @param code The {@link KapuaErrorCode} to search in the {@link ResourceBundle}. | ||
* @param args The arguments to use to populate the message pattern. | ||
* @return The localized message for the given {@link KapuaErrorCode}. | ||
* @since 1.0.0 | ||
*/ | ||
public static String getLocalizedMessage(String resourceBundleName, Locale locale, KapuaErrorCode code, Object[] args) { | ||
|
||
String pattern = getMessagePattern(resourceBundleName, locale, code); | ||
if (pattern != null) { | ||
return MessageFormat.format(pattern, args); | ||
} else { | ||
StringJoiner joiner = new StringJoiner(", "); | ||
if (args != null && args.length > 0) { | ||
for (Object arg : args) { | ||
joiner.add(String.valueOf(arg)); | ||
} | ||
} | ||
|
||
return MessageFormat.format(KAPUA_GENERIC_MESSAGE, joiner.toString()); | ||
} | ||
} | ||
|
||
/** | ||
* Loads the message pattern from the resource bundle. | ||
* | ||
* @param resourceBundleName The {@link ResourceBundle} name from which to search the {@link KapuaErrorCode}. | ||
* @param locale The {@link Locale} of the {@link ResourceBundle}. | ||
* @param code The {@link KapuaErrorCode} to search in the {@link ResourceBundle}. | ||
* @return The message pattern of the given {@link KapuaErrorCode} if found, {@code null} otherwise. | ||
* @since 1.0.0 | ||
*/ | ||
private static String getMessagePattern(@NotNull String resourceBundleName, @NotNull Locale locale, @NotNull KapuaErrorCode code) { | ||
|
||
String messagePattern = null; | ||
ResourceBundle resourceBundle; | ||
try { | ||
resourceBundle = ResourceBundle.getBundle(resourceBundleName, locale); | ||
} catch (MissingResourceException mre) { | ||
LOG.warn("Could not load exception messages for resource: {} in locale: {}. A generic error message will be printed.", resourceBundleName, locale); | ||
return null; | ||
} | ||
try { | ||
messagePattern = resourceBundle.getString(code.name()); | ||
} catch (MissingResourceException mre) { | ||
LOG.warn("Could not load exception messages for code: {}. A generic error message will be printed.", code.name()); | ||
} | ||
|
||
return messagePattern; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.