|
|
|
@@ -1,36 +1,56 @@
|
|
|
|
|
from strutils import isEmptyOrWhitespace
|
|
|
|
|
from httpcore import HttpCode
|
|
|
|
|
import std/[httpcore, json, options, strutils]
|
|
|
|
|
|
|
|
|
|
type ApiError* = object of CatchableError
|
|
|
|
|
respMsg*: string
|
|
|
|
|
respCode*: HttpCode
|
|
|
|
|
respData*: Option[JsonNode]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc newApiError*(parent: ref Exception = nil, respCode: HttpCode, respMsg: string, msg = ""): ref ApiError =
|
|
|
|
|
proc newApiError*(
|
|
|
|
|
parent: ref Exception = nil,
|
|
|
|
|
respCode: HttpCode,
|
|
|
|
|
respMsg: string,
|
|
|
|
|
respData = none[JsonNode](),
|
|
|
|
|
msg = ""): ref ApiError =
|
|
|
|
|
result = newException(ApiError, msg, parent)
|
|
|
|
|
result.respCode = respCode
|
|
|
|
|
result.respMsg = respMsg
|
|
|
|
|
result.respData = respData
|
|
|
|
|
|
|
|
|
|
if not parent.isNil:
|
|
|
|
|
result.trace &= parent.trace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc raiseApiError*(respCode: HttpCode, respMsg: string, msg = "") =
|
|
|
|
|
proc raiseApiError*(
|
|
|
|
|
respCode: HttpCode,
|
|
|
|
|
respMsg: string,
|
|
|
|
|
respData = none[JsonNode](),
|
|
|
|
|
msg = "") =
|
|
|
|
|
var apiError = newApiError(
|
|
|
|
|
parent = nil,
|
|
|
|
|
respCode = respCode,
|
|
|
|
|
respMsg = respMsg,
|
|
|
|
|
respData = respData,
|
|
|
|
|
msg = if msg.isEmptyOrWhitespace: respMsg
|
|
|
|
|
else: msg)
|
|
|
|
|
raise apiError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc raiseApiError*(respCode: HttpCode, parent: ref Exception, respMsg: string = "", msg = "") =
|
|
|
|
|
proc raiseApiError*(
|
|
|
|
|
respCode: HttpCode,
|
|
|
|
|
parent: ref Exception,
|
|
|
|
|
respMsg: string = "",
|
|
|
|
|
respData = none[JsonNode](),
|
|
|
|
|
msg = "") =
|
|
|
|
|
var apiError = newApiError(
|
|
|
|
|
parent = parent,
|
|
|
|
|
respCode = respCode,
|
|
|
|
|
respMsg =
|
|
|
|
|
if respMsg.isEmptyOrWhitespace: parent.msg
|
|
|
|
|
else: respMsg,
|
|
|
|
|
msg = msg)
|
|
|
|
|
respData = respData,
|
|
|
|
|
msg =
|
|
|
|
|
if msg.isEmptyOrWhitespace: parent.msg
|
|
|
|
|
else: msg)
|
|
|
|
|
|
|
|
|
|
raise apiError
|
|
|
|
|