forked from opensearch-project/common-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added validation for the clusters field. Refactored ClusterMetricsInp…
…ut validiation to throw 4xx-level CommonUtilsExceptions instead of 5xx-level IllegalArgumentException. Signed-off-by: AWSHurneyt <[email protected]>
- Loading branch information
1 parent
c05715b
commit 04ddb3d
Showing
3 changed files
with
258 additions
and
46 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
71 changes: 71 additions & 0 deletions
71
src/main/kotlin/org/opensearch/commons/alerting/util/CommonUtilsException.kt
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,71 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.commons.alerting.util | ||
|
||
import org.apache.logging.log4j.LogManager | ||
import org.opensearch.OpenSearchException | ||
import org.opensearch.OpenSearchSecurityException | ||
import org.opensearch.OpenSearchStatusException | ||
import org.opensearch.core.common.Strings | ||
import org.opensearch.core.rest.RestStatus | ||
import org.opensearch.index.IndexNotFoundException | ||
import org.opensearch.index.engine.VersionConflictEngineException | ||
import org.opensearch.indices.InvalidIndexNameException | ||
|
||
private val log = LogManager.getLogger(CommonUtilsException::class.java) | ||
|
||
class CommonUtilsException(message: String, val status: RestStatus, ex: Exception) : OpenSearchException(message, ex) { | ||
|
||
override fun status(): RestStatus { | ||
return status | ||
} | ||
|
||
companion object { | ||
|
||
@JvmStatic | ||
fun wrap(ex: Exception): OpenSearchException { | ||
log.error("Common utils error: $ex") | ||
|
||
var friendlyMsg = "Unknown error" | ||
var status = RestStatus.INTERNAL_SERVER_ERROR | ||
when (ex) { | ||
is IndexNotFoundException -> { | ||
status = ex.status() | ||
friendlyMsg = "Configured indices are not found: ${ex.index}" | ||
} | ||
is OpenSearchSecurityException -> { | ||
status = ex.status() | ||
friendlyMsg = "User doesn't have permissions to execute this action. Contact administrator." | ||
} | ||
is OpenSearchStatusException -> { | ||
status = ex.status() | ||
friendlyMsg = ex.message as String | ||
} | ||
is IllegalArgumentException -> { | ||
status = RestStatus.BAD_REQUEST | ||
friendlyMsg = ex.message as String | ||
} | ||
is VersionConflictEngineException -> { | ||
status = ex.status() | ||
friendlyMsg = ex.message as String | ||
} | ||
is InvalidIndexNameException -> { | ||
status = RestStatus.BAD_REQUEST | ||
friendlyMsg = ex.message as String | ||
} | ||
else -> { | ||
if (!Strings.isNullOrEmpty(ex.message)) { | ||
friendlyMsg = ex.message as String | ||
} | ||
} | ||
} | ||
// Wrapping the origin exception as runtime to avoid it being formatted. | ||
// Currently, alerting-kibana is using `error.root_cause.reason` as text in the toast message. | ||
// Below logic is to set friendly message to error.root_cause.reason. | ||
return CommonUtilsException(friendlyMsg, status, Exception("${ex.javaClass.name}: ${ex.message}")) | ||
} | ||
} | ||
} |
Oops, something went wrong.