summaryrefslogtreecommitdiff
path: root/bank/src/main/kotlin/tech/libeufin/bank/Error.kt
blob: 04c3881e9d974095901f1a9d0c4d25f4ecedf0a5 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2023 Taler Systems S.A.

 * LibEuFin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.

 * LibEuFin is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
 * Public License for more details.

 * You should have received a copy of the GNU Affero General Public
 * License along with LibEuFin; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>
 */
package tech.libeufin.bank

import io.ktor.http.*
import io.ktor.server.response.*
import io.ktor.server.application.ApplicationCall
import io.ktor.util.AttributeKey
import kotlinx.serialization.Serializable
import net.taler.common.errorcodes.TalerErrorCode
import tech.libeufin.util.*
/**
 * Convenience type to throw errors along the bank activity
 * and that is meant to be caught by Ktor and responded to the
 * client.
 */
class LibeufinException(
    // Status code that Ktor will set for the response.
    val httpStatus: HttpStatusCode,
    // Error detail object, after Taler API.
    val talerError: TalerError
) : Exception(talerError.hint)

/**
 * Error object to respond to the client.  The
 * 'code' field takes values from the GANA gnu-taler-error-code
 * specification.  'hint' is a human-readable description
 * of the error.
 */
@Serializable
data class TalerError(
    @kotlinx.serialization.Transient val err: TalerErrorCode = TalerErrorCode.END,
    val code: Int,
    val hint: String? = null,
    val detail: String? = null
)

private val LOG_MSG = AttributeKey<String>("log_msg");

fun ApplicationCall.logMsg(): String? = attributes.getOrNull(LOG_MSG)

suspend fun ApplicationCall.err(
    status: HttpStatusCode,
    hint: String?,
    error: TalerErrorCode
) {
    err(
        LibeufinException(
            httpStatus = status, talerError = TalerError(
                code = error.code, err = error, hint = hint
            )
        )
    )
}

suspend fun ApplicationCall.err(
    err: LibeufinException
) {
    attributes.put(LOG_MSG, "${err.talerError.err.name} ${err.talerError.hint}")
    respond(
        status = err.httpStatus,
        message = err.talerError
    )
}


fun libeufinError(
    status: HttpStatusCode,
    hint: String?,
    error: TalerErrorCode,
    detail: String? = null
): LibeufinException = LibeufinException(
    httpStatus = status, talerError = TalerError(
        code = error.code, err = error, hint = hint, detail = detail
    )
)

/* ----- HTTP error ----- */

fun forbidden(
    hint: String = "No rights on the resource",
    error: TalerErrorCode = TalerErrorCode.END
): LibeufinException = libeufinError(HttpStatusCode.Forbidden, hint, error)

fun unauthorized(
    hint: String,
    error: TalerErrorCode = TalerErrorCode.GENERIC_UNAUTHORIZED
): LibeufinException = libeufinError(HttpStatusCode.Unauthorized, hint, error)

fun internalServerError(hint: String?): LibeufinException 
    = libeufinError(HttpStatusCode.InternalServerError, hint, TalerErrorCode.GENERIC_INTERNAL_INVARIANT_FAILURE)

fun notFound(
    hint: String,
    error: TalerErrorCode
): LibeufinException = libeufinError(HttpStatusCode.NotFound, hint, error)

fun conflict(
    hint: String, error: TalerErrorCode
): LibeufinException = libeufinError(HttpStatusCode.Conflict, hint, error)

fun badRequest(
    hint: String? = null, 
    error: TalerErrorCode = TalerErrorCode.GENERIC_JSON_INVALID,
    detail: String? = null
): LibeufinException = libeufinError(HttpStatusCode.BadRequest, hint, error, detail)


/* ----- Currency checks ----- */

fun BankConfig.checkRegionalCurrency(amount: TalerAmount) {
    if (amount.currency != regionalCurrency) throw badRequest(
        "Wrong currency: expected regional currency $regionalCurrency got ${amount.currency}",
        TalerErrorCode.GENERIC_CURRENCY_MISMATCH
    )
}

fun BankConfig.checkFiatCurrency(amount: TalerAmount) {
    if (amount.currency != fiatCurrency) throw badRequest(
        "Wrong currency: expected fiat currency $fiatCurrency got ${amount.currency}",
        TalerErrorCode.GENERIC_CURRENCY_MISMATCH
    )
}

/* ----- Common errors ----- */

fun unknownAccount(id: String): LibeufinException {
    return notFound(
        "Account '$id' not found",
        TalerErrorCode.BANK_UNKNOWN_ACCOUNT
    )
}

fun unsupportedTanChannel(channel: TanChannel): LibeufinException {
    return conflict(
        "Unsupported tan channel $channel",
        TalerErrorCode.BANK_TAN_CHANNEL_NOT_SUPPORTED
    )
}