mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-04-27 18:50:29 -04:00
trying to be clever in error handling didn't work.... will fix later
This commit is contained in:
parent
ea758d5004
commit
d7099fbed0
@ -2,79 +2,82 @@ const axios = require("axios");
|
|||||||
|
|
||||||
exports.handler = function (event, context, callback) {
|
exports.handler = function (event, context, callback) {
|
||||||
try {
|
try {
|
||||||
// if triggered as an image without JS (i.e. from AMP pages) set `?noscript=true`
|
// attach a timer to axios to debug response time from endpoint
|
||||||
const noScript = event.queryStringParameters["noscript"] === "true";
|
// https://stackoverflow.com/questions/62186171/measure-network-latency-in-react-native/62257712#62257712
|
||||||
|
(function (instance) {
|
||||||
|
instance.interceptors.request.use((request) => {
|
||||||
|
request.startTime = Date.now();
|
||||||
|
return request;
|
||||||
|
});
|
||||||
|
|
||||||
// https://docs.simpleanalytics.com/without-javascript
|
instance.interceptors.response.use((response) => {
|
||||||
const endpointHost = "queue.simpleanalyticscdn.com";
|
response.elapsedTime = Number(Date.now() - response.config.startTime).toFixed();
|
||||||
const endpointPath = noScript ? "noscript.gif" : "simple.gif";
|
return response;
|
||||||
const endpointUrl = "https://" + endpointHost + "/" + endpointPath;
|
});
|
||||||
|
})(axios);
|
||||||
|
|
||||||
// pass these headers along to SA
|
// pass these URL parameters along to endpoint
|
||||||
|
const reqQuery = event.queryStringParameters;
|
||||||
|
reqQuery["ignore-dnt"] = "true"; // this isn't nefarious, we're not tracking in the first place!
|
||||||
|
|
||||||
|
// pass these optional headers along to endpoint
|
||||||
const reqHeaders = {
|
const reqHeaders = {
|
||||||
referer: event.headers["referer"] || "",
|
referer: event.headers["referer"] || "",
|
||||||
"user-agent": event.headers["user-agent"] || "",
|
"user-agent": event.headers["user-agent"] || "",
|
||||||
};
|
};
|
||||||
|
|
||||||
// pass these URL parameters along to SA
|
// if triggered as an image without JS (i.e. from AMP pages) set `?noscript=true`
|
||||||
const reqQuery = event.queryStringParameters;
|
// https://docs.simpleanalytics.com/without-javascript
|
||||||
reqQuery["ignore-dnt"] = "true"; // this isn't nefarious, we're not tracking in the first place!
|
const endpointHost = "queue.simpleanalyticscdn.com";
|
||||||
|
const endpointPath = reqQuery["noscript"] === "true" ? "noscript.gif" : "simple.gif";
|
||||||
const reqConfig = {
|
const endpointUrl = "https://" + endpointHost + "/" + endpointPath;
|
||||||
method: "GET",
|
|
||||||
url: endpointUrl,
|
|
||||||
headers: reqHeaders,
|
|
||||||
params: reqQuery,
|
|
||||||
responseType: "arraybuffer",
|
|
||||||
timeout: 2000,
|
|
||||||
};
|
|
||||||
|
|
||||||
console.info(`Proxying ${reqQuery["type"]} to ${endpointPath} ...`);
|
|
||||||
|
|
||||||
axios
|
axios
|
||||||
.request(reqConfig)
|
.request({
|
||||||
|
method: "GET",
|
||||||
|
url: endpointUrl,
|
||||||
|
headers: reqHeaders,
|
||||||
|
params: reqQuery,
|
||||||
|
responseType: "arraybuffer",
|
||||||
|
timeout: 2000,
|
||||||
|
})
|
||||||
.then(function (response) {
|
.then(function (response) {
|
||||||
// parse the feedback message from SA
|
console.info(
|
||||||
|
`${response.status} ${response.statusText} | ${response.elapsedTime}ms | ${response.headers["simple-analytics-feedback"]}`
|
||||||
|
);
|
||||||
|
|
||||||
|
// parse the feedback message from endpoint
|
||||||
const apiFeedback = response.headers["simple-analytics-feedback"] || "No feedback from Simple Analytics.";
|
const apiFeedback = response.headers["simple-analytics-feedback"] || "No feedback from Simple Analytics.";
|
||||||
const shortFeedback = apiFeedback.toLowerCase().includes("thanks for sending") ? "OK" : `ERROR: ${apiFeedback}`;
|
const shortFeedback = apiFeedback.toLowerCase().includes("thanks for sending ")
|
||||||
|
? "OK"
|
||||||
console.info(response.status, apiFeedback);
|
: `ERROR: ${apiFeedback}`;
|
||||||
|
|
||||||
// imitate the headers that would normally be sent back from SA's pixel
|
|
||||||
const resHeaders = {
|
|
||||||
"Content-Type": "image/gif",
|
|
||||||
"Cache-Control": "private, no-cache, no-store, must-revalidate",
|
|
||||||
Expires: "0",
|
|
||||||
Pragma: "no-cache",
|
|
||||||
"X-API-Response": shortFeedback,
|
|
||||||
"X-API-Endpoint": endpointHost,
|
|
||||||
};
|
|
||||||
|
|
||||||
// reasoning for base64 encoding:
|
// reasoning for base64 encoding:
|
||||||
// https://community.netlify.com/t/debugging-a-function-returns-502/429/12
|
// https://community.netlify.com/t/debugging-a-function-returns-502/429/12
|
||||||
callback(null, {
|
callback(null, {
|
||||||
statusCode: response.status,
|
statusCode: response.status,
|
||||||
headers: resHeaders,
|
headers: {
|
||||||
|
"Content-Type": "image/gif",
|
||||||
|
"Cache-Control": "private, no-cache, no-store, must-revalidate",
|
||||||
|
Expires: "0",
|
||||||
|
Pragma: "no-cache",
|
||||||
|
"x-api-response": shortFeedback,
|
||||||
|
"x-api-latency": response.elapsedTime,
|
||||||
|
},
|
||||||
body: response.data.toString("base64"),
|
body: response.data.toString("base64"),
|
||||||
isBase64Encoded: true,
|
isBase64Encoded: true,
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(function (error) {
|
.catch(function (error) {
|
||||||
// this indicates a function error, NOT an error returned from SA
|
// this indicates a request error, NOT an error in the endpoint's reponse
|
||||||
throw error;
|
console.error(error.message);
|
||||||
|
|
||||||
|
callback(Error(error));
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// something went VERY wrong...
|
// something went VERY wrong...
|
||||||
console.log(error.message);
|
console.error(error.message);
|
||||||
|
|
||||||
callback(null, {
|
callback(Error(error));
|
||||||
statusCode: 500,
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
error: error.message,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user