@@ -11,38 +11,44 @@ data class ErrorResponse(
1111 val fieldValue : String? = null ,
1212)
1313
14- sealed class ApiError (
15- protected open val errorResponse : ErrorResponse ,
16- ) {
17- open fun messageMap (): Map <String , ErrorResponse > = mapOf (" error" to errorResponse)
18-
19- fun status () = this .errorResponse.status
14+ sealed interface ApiError {
15+ val response: ErrorResponse
2016}
2117
18+ fun ApiError.status () = response.status
19+
20+ fun ApiError.messageMap (): Map <String , ErrorResponse > =
21+ when (this ) {
22+ is UpstreamError -> mapOf (" upstream" to upstream, " error" to response)
23+
24+ is RequiredField ,
25+ is CallPrincipalMissing ,
26+ is TokenMissing ,
27+ is TokenMissingUser ,
28+ is RefreshTokenInvalid ,
29+ -> mapOf (" error" to response)
30+ }
31+
2232abstract class UpstreamError (
2333 open val upstream : ErrorResponse ,
2434 val systemName : String ,
25- ) : ApiError(
35+ ) : ApiError {
36+ override val response =
2637 ErrorResponse (
2738 status = HttpStatusCode .InternalServerError ,
2839 message = " call to $systemName failed" ,
29- ),
30- ) {
31- override fun messageMap () =
32- mapOf (
33- " upstream" to upstream,
34- " error" to errorResponse,
3540 )
3641}
3742
3843abstract class RequiredField (
3944 val fieldName : String ,
40- ) : ApiError(
45+ ) : ApiError {
46+ override val response =
4147 ErrorResponse (
4248 status = HttpStatusCode .BadRequest ,
4349 message = " $fieldName required" ,
44- ),
45- )
50+ )
51+ }
4652
4753data object ConferenceIdRequired : RequiredField (fieldName = " id" )
4854
@@ -53,30 +59,41 @@ data class SleepingPillCallFailed(
5359 systemName = " SleepingPill" ,
5460 )
5561
56- data object CallPrincipalMissing : ApiError (
57- ErrorResponse (
58- status = HttpStatusCode . Unauthorized ,
59- message = " Principal missing" ,
60- ) ,
61- )
62+ data class CognitoCallFailed (
63+ override val upstream : ErrorResponse ,
64+ ) : UpstreamError(
65+ upstream = upstream ,
66+ systemName = " Cognito " ,
67+ )
6268
63- data object TokenMissing : ApiError (
64- ErrorResponse (
65- status = HttpStatusCode .Unauthorized ,
66- message = "Principal missing token",
67- ),
68- )
69+ data object CallPrincipalMissing : ApiError {
70+ override val response =
71+ ErrorResponse (
72+ status = HttpStatusCode .Unauthorized ,
73+ message = " Principal missing" ,
74+ )
75+ }
6976
70- data object TokenMissingUser : ApiError (
71- ErrorResponse (
72- status = HttpStatusCode .Unauthorized ,
73- message = "User missing in token",
74- ),
75- )
77+ data object TokenMissing : ApiError {
78+ override val response =
79+ ErrorResponse (
80+ status = HttpStatusCode .Unauthorized ,
81+ message = " Principal missing token" ,
82+ )
83+ }
7684
77- data object RefreshTokenInvalid : ApiError (
78- ErrorResponse (
79- status = HttpStatusCode .Unauthorized ,
80- message = "Refresh token is invalid or has expired",
81- ),
82- )
85+ data object TokenMissingUser : ApiError {
86+ override val response =
87+ ErrorResponse (
88+ status = HttpStatusCode .Unauthorized ,
89+ message = " User missing in token" ,
90+ )
91+ }
92+
93+ data object RefreshTokenInvalid : ApiError {
94+ override val response =
95+ ErrorResponse (
96+ status = HttpStatusCode .Unauthorized ,
97+ message = " Refresh token is invalid or has expired" ,
98+ )
99+ }
0 commit comments