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 new clusters field. (opensearch-project#633)
* Added validation for the clusters field. Refactored ClusterMetricsInput validiation to throw 4xx-level CommonUtilsExceptions instead of 5xx-level IllegalArgumentException. Signed-off-by: AWSHurneyt <[email protected]> * Moved some regex from alerting plugin to common utils. Signed-off-by: AWSHurneyt <[email protected]> * Moved cluster-based regex to separate file. Signed-off-by: AWSHurneyt <[email protected]> * Fixed ktlint error. Signed-off-by: AWSHurneyt <[email protected]> * Fixed regex. Moved cluster-related regexes. Signed-off-by: AWSHurneyt <[email protected]> --------- Signed-off-by: AWSHurneyt <[email protected]>
- Loading branch information
1 parent
9beae87
commit abc69cd
Showing
5 changed files
with
287 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}")) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.