-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigValidationResult.scala
71 lines (58 loc) · 2.6 KB
/
ConfigValidationResult.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package za.co.absa.loginsvc.rest.config.validation
import org.slf4j.LoggerFactory
sealed trait ConfigValidationResult {
private val logger = LoggerFactory.getLogger(classOf[ConfigValidatable])
def errors: Seq[ConfigValidationException]
def merge(anotherResult: ConfigValidationResult): ConfigValidationResult = ConfigValidationResult.merge(this, anotherResult)
/**
* Convenience method to turn a ConfigValidationResult into throwing an exception if present
* @throws ConfigValidationException for the first error if there any
*/
@throws(classOf[ConfigValidationException])
def throwOnErrors(): Unit = {
errors.headOption.foreach { firstError =>
// all errors are logged
logger.error(s"validation errors (${errors.length}):\n ${errors.mkString(",\n ")}")
// but only the first is in the error stack
throw firstError
}
}
}
object ConfigValidationResult {
def merge(res1: ConfigValidationResult, res2: ConfigValidationResult): ConfigValidationResult = (res1, res2) match {
case (ConfigValidationSuccess, ConfigValidationSuccess) => ConfigValidationSuccess
case (ConfigValidationSuccess, e@ConfigValidationError(_)) => e
case (e@ConfigValidationError(_), ConfigValidationSuccess) => e
case (ConfigValidationError(rs1), ConfigValidationError(rs2)) => ConfigValidationError(rs1 ++ rs2)
}
case object ConfigValidationSuccess extends ConfigValidationResult {
override def errors: Seq[ConfigValidationException] = Seq.empty
}
case class ConfigValidationError(reasons: Seq[ConfigValidationException]) extends ConfigValidationResult {
override def errors: Seq[ConfigValidationException] = reasons
}
case object ConfigValidationError {
/**
* Shorthand of za.co.absa.loginsvc.rest.config.validation.ConfigValidationException(Seq(reason)
*
* @param reason validation exception
* @return ConfigValidationException
*/
def apply(reason: ConfigValidationException): ConfigValidationError = new ConfigValidationError(Seq(reason))
}
}