aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuicommon/error/DomainError.scala
blob: c76141473abe7a5504266d8f31d4389d4b6a7326 (plain) (blame)
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
package xyz.driver.pdsuicommon.error

import xyz.driver.pdsuicommon.logging.{PhiString, Unsafe}
import xyz.driver.pdsuicommon.utils.Utils

trait DomainError {

  protected def userMessage: String

  def getMessage: String = userMessage

}

object DomainError {

  // 404 error
  trait NotFoundError extends DomainError

  // 401 error
  trait AuthenticationError extends DomainError

  // 403 error
  trait AuthorizationError extends DomainError

  implicit def toPhiString(x: DomainError): PhiString = {
    // userMessage possibly can contain a personal information,
    // so we should prevent it to be printed in logs.
    Unsafe(Utils.getClassSimpleName(x.getClass))
  }
}

/** Subclasses of this exception correspond to subclasses of DomainError. They
  * are used in REST service implementations to fail futures rather than
  * returning successful futures, completed with corresponding DomainErrors. */
// scalastyle:off null
@SuppressWarnings(Array("org.wartremover.warts.Null"))
class DomainException(message: String, cause: Throwable = null) extends RuntimeException(message, cause)
class NotFoundException(message: String)                        extends DomainException(message) // 404
class AuthenticationException(message: String)                  extends DomainException(message) // 401
class AuthorizationException(message: String)                   extends DomainException(message) // 403
// scalastyle:on null