1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-27 07:38:29 -04:00

catch and log lambda errors

This commit is contained in:
Jake Jarvis 2021-05-30 09:58:29 -04:00
parent ba81de8765
commit 1d76ff93ad
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
2 changed files with 70 additions and 53 deletions

View File

@ -18,14 +18,22 @@
fetch("/api/hits?slug=" + slug) fetch("/api/hits?slug=" + slug)
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
// finally inject the hits and hide the loading spinner if (typeof data.hits !== "undefined") {
var spinner = document.getElementById("hit-spinner"); // finally inject the hits and hide the loading spinner
var counter = document.getElementById("hit-counter"); var spinner = document.getElementById("hit-spinner");
var counter = document.getElementById("hit-counter");
spinner.style.display = "none"; spinner.style.display = "none";
wrapper.title = data.pretty_hits + " " + data.pretty_unit; wrapper.title = data.pretty_hits + " " + data.pretty_unit;
counter.appendChild(document.createTextNode(data.pretty_hits)); counter.appendChild(document.createTextNode(data.pretty_hits));
} else {
// something went horribly wrong, initiate coverup
wrapper.style.display = "none";
}
}) })
.catch((error) => {}); .catch((error) => {
// something went horribly wrong, initiate coverup
wrapper.style.display = "none";
});
} }
})(); })();

View File

@ -1,3 +1,5 @@
"use strict";
const faunadb = require("faunadb"), const faunadb = require("faunadb"),
q = faunadb.query; q = faunadb.query;
const numeral = require("numeral"); const numeral = require("numeral");
@ -9,12 +11,8 @@ require("dotenv").config();
require("encoding"); require("encoding");
exports.handler = async (event, context) => { exports.handler = async (event, context) => {
const { slug } = event.queryStringParameters;
const client = new faunadb.Client({
secret: process.env.FAUNADB_SERVER_SECRET,
});
// some rudimentary error handling // some rudimentary error handling
const { slug } = event.queryStringParameters;
if (!slug || slug === "/") { if (!slug || slug === "/") {
return { return {
statusCode: 400, statusCode: 400,
@ -24,51 +22,62 @@ exports.handler = async (event, context) => {
}; };
} }
const result = await client.query( try {
q.Let( const client = new faunadb.Client({
{ secret: process.env.FAUNADB_SERVER_SECRET,
match: q.Match(q.Index("hits_by_slug"), slug), });
},
q.If( const result = await client.query(
q.Exists(q.Var("match")), q.Let(
q.Let( {
{ match: q.Match(q.Index("hits_by_slug"), slug),
ref: q.Select("ref", q.Get(q.Var("match"))), },
hits: q.ToInteger(q.Select("hits", q.Select("data", q.Get(q.Var("match"))))), q.If(
}, q.Exists(q.Var("match")),
q.Update(q.Var("ref"), { q.Let(
{
ref: q.Select("ref", q.Get(q.Var("match"))),
hits: q.ToInteger(q.Select("hits", q.Select("data", q.Get(q.Var("match"))))),
},
q.Update(q.Var("ref"), {
data: {
hits: q.Add(q.Var("hits"), 1),
},
})
),
q.Create(q.Collection("hits"), {
data: { data: {
hits: q.Add(q.Var("hits"), 1), slug: slug,
hits: 1,
}, },
}) })
), )
q.Create(q.Collection("hits"), {
data: {
slug: slug,
hits: 1,
},
})
) )
) );
);
client.close(); // send client the new hit count
return {
statusCode: 200,
headers: {
"Cache-Control": "private, no-cache, no-store, must-revalidate",
Expires: "0",
Pragma: "no-cache",
},
body: JSON.stringify({
slug: result.data.slug,
hits: result.data.hits,
pretty_hits: numeral(result.data.hits).format("0,0"),
pretty_unit: pluralize("hit", result.data.hits),
}),
};
} catch (error) {
console.error(error);
const hits = result.data.hits; return {
statusCode: 400,
// send client the new hit count body: JSON.stringify({
return { message: error.message,
statusCode: 200, }),
headers: { };
"Cache-Control": "private, no-cache, no-store, must-revalidate", }
Expires: "0",
Pragma: "no-cache",
},
body: JSON.stringify({
slug: slug,
hits: hits,
pretty_hits: numeral(hits).format("0,0"),
pretty_unit: pluralize("hit", hits),
}),
};
}; };