Skip to content

Commit

Permalink
feat: add origin ip and did when available to anchor err (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachferland authored May 25, 2023
1 parent 1664a70 commit 1b0d655
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/controllers/request-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ import { isLeft, report, string, strict, validate } from 'codeco'
* If no header found, use IP address of the requester.
*/
function parseOrigin(req: ExpReq): string {
const didHeader = req.get('did')
const didHeader = parseOriginDID(req)
if (didHeader) return didHeader
return parseOriginIP(req)
}

function parseOriginIP(req: ExpReq): string {
const sourceIp = req.get('sourceIp')
if (sourceIp) return sourceIp
let addresses = req.ip
Expand All @@ -43,6 +47,10 @@ function parseOrigin(req: ExpReq): string {
return addressesSplit[0].trim()
}

function parseOriginDID(req: ExpReq): string | undefined {
return req.get('did')
}

const GetStatusParams = strict({
cid: string.pipe(cidAsString),
})
Expand Down Expand Up @@ -113,17 +121,18 @@ export class RequestController {
return res.status(StatusCodes.CREATED).json(body)
} catch (err: any) {
Metrics.count(METRIC_NAMES.REQUEST_NOT_CREATED, 1, { source: parseOrigin(req) })
return this.getBadRequestResponse(res, err, requestParams, origin)
return this.getBadRequestResponse(res, err, requestParams, parseOriginDID(req) || 'none', parseOriginIP(req))
}
}

private getBadRequestResponse(
res: ExpRes,
err: Error,
requestParams: RequestAnchorParams,
origin: string
originDID: string,
originIP: string
): ExpRes {
const errmsg = `Creating request with streamId ${requestParams.streamId} and commit CID ${requestParams.cid} from ${origin} failed: ${err.message}`
const errmsg = `Creating request with streamId ${requestParams.streamId} and commit CID ${requestParams.cid} from IP: ${originIP} and DID: ${originDID} failed: ${err.message}`
logger.err(errmsg)
logger.err(err) // Log stack trace
return res.status(StatusCodes.BAD_REQUEST).json({
Expand Down

0 comments on commit 1b0d655

Please sign in to comment.