Skip to content

Commit

Permalink
Attempt to log 400 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
johannbm committed Apr 18, 2024
1 parent 86140ef commit c8ad36b
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions apps/frackend/src/apiProxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { getToken, requestOboToken } from "@navikt/oasis";
import { Express, NextFunction, Request, Response } from "express";
import { createProxyMiddleware } from "http-proxy-middleware";
import {
createProxyMiddleware,
responseInterceptor,
} from "http-proxy-middleware";

import config from "./config.js";

Expand Down Expand Up @@ -47,6 +50,7 @@ export function addProxyHandler(
target: outgoingUrl,
changeOrigin: true,
logger: console,
selfHandleResponse: true,
on: {
proxyReq: (proxyRequest, request) => {
const obo = request.headers["obo-token"];
Expand All @@ -58,15 +62,27 @@ export function addProxyHandler(
);
}
},
proxyRes: (proxyResponse, request) => {
if (proxyResponse.statusCode === 400) {
console.log(
"[ProxyRes 400]",
"user-agent",
request.headers["user-agent"],
);
}
},
proxyRes: responseInterceptor(
async (responseBuffer, proxyResponse, request, response) => {
if (response.statusCode === 400) {
console.log(
"[400 RESPONSE] contentType",
proxyResponse.headers["content-type"],
);
if (
proxyResponse.headers["content-type"] === "application/json"
) {
const data = JSON.parse(responseBuffer.toString("utf8"));

console.log("[400 RESPONSE] DATA", data);

return JSON.stringify(data);
}
}

return responseBuffer;
},
),
},
}),
);
Expand Down

0 comments on commit c8ad36b

Please sign in to comment.