3 Commits

3 changed files with 63 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
# Package # Package
version = "0.4.8" version = "0.4.11"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Jonathan's opinionated extensions and auth layer for Jester." description = "Jonathan's opinionated extensions and auth layer for Jester."
license = "MIT" license = "MIT"

View File

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

View File

@@ -1,5 +1,5 @@
import std/[json, jsonutils, options, sequtils, strtabs, strutils] import std/[json, jsonutils, options, sequtils, strtabs, strutils]
import mummy, webby import mummy, webby, uuids
import std/httpcore except HttpHeaders import std/httpcore except HttpHeaders
@@ -31,7 +31,7 @@ func initApiResponse*[T](
totalItems: totalItems, nextLink: nextLink, prevLink: prevLink) totalItems: totalItems, nextLink: nextLink, prevLink: prevLink)
func `%`*(r: ApiResponse): JsonNode = proc `%`*(r: ApiResponse): JsonNode =
result = newJObject() result = newJObject()
if r.details.isSome: result["details"] = %r.details if r.details.isSome: result["details"] = %r.details
if r.data.isSome: result["data"] = %r.data if r.data.isSome: result["data"] = %r.data
@@ -90,6 +90,40 @@ func origin*(req: Request): Option[string] =
else: none[string]() else: none[string]()
func traceparent*(req: Request): Option[string] =
## Extract the traceparent from the request headers, if present.
if req.headers.contains("traceparent"):
return some(req.headers["traceparent"])
else:
return none[string]()
proc makeTraceContextHeaders*(req: Request, traceParentId: string): HttpHeaders =
var headers = HttpHeaders(@[])
if req.headers.contains("traceparent"):
# If the traceparent header is present, we should update it with our
# parent-id.
let traceparentParts = req.headers["traceparent"].split("-")
if traceparentParts.len != 4:
headers["traceparent"] = "00-$#-$#-00" % [
replace($genUUID(), "-", ""), # trace-id
traceParentId, # parent-id
]
else:
headers["traceparent"] = "00-$#-$#-$#" % [
traceparentParts[1], # trace-id
traceParentId, # parent-id
traceparentParts[3], # flags
]
if req.headers.contains("tracestate"):
headers["tracestate"] = req.headers["tracestate"]
return headers
proc respondWithRawJson*( proc respondWithRawJson*(
req: Request, req: Request,
body: JsonNode, body: JsonNode,