mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-04-26 23:48:27 -04:00
catch and log lambda errors
This commit is contained in:
parent
ba81de8765
commit
1d76ff93ad
@ -18,14 +18,22 @@
|
||||
fetch("/api/hits?slug=" + slug)
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
// finally inject the hits and hide the loading spinner
|
||||
var spinner = document.getElementById("hit-spinner");
|
||||
var counter = document.getElementById("hit-counter");
|
||||
if (typeof data.hits !== "undefined") {
|
||||
// finally inject the hits and hide the loading spinner
|
||||
var spinner = document.getElementById("hit-spinner");
|
||||
var counter = document.getElementById("hit-counter");
|
||||
|
||||
spinner.style.display = "none";
|
||||
wrapper.title = data.pretty_hits + " " + data.pretty_unit;
|
||||
counter.appendChild(document.createTextNode(data.pretty_hits));
|
||||
spinner.style.display = "none";
|
||||
wrapper.title = data.pretty_hits + " " + data.pretty_unit;
|
||||
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";
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
@ -1,3 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
const faunadb = require("faunadb"),
|
||||
q = faunadb.query;
|
||||
const numeral = require("numeral");
|
||||
@ -9,12 +11,8 @@ require("dotenv").config();
|
||||
require("encoding");
|
||||
|
||||
exports.handler = async (event, context) => {
|
||||
const { slug } = event.queryStringParameters;
|
||||
const client = new faunadb.Client({
|
||||
secret: process.env.FAUNADB_SERVER_SECRET,
|
||||
});
|
||||
|
||||
// some rudimentary error handling
|
||||
const { slug } = event.queryStringParameters;
|
||||
if (!slug || slug === "/") {
|
||||
return {
|
||||
statusCode: 400,
|
||||
@ -24,51 +22,62 @@ exports.handler = async (event, context) => {
|
||||
};
|
||||
}
|
||||
|
||||
const result = await client.query(
|
||||
q.Let(
|
||||
{
|
||||
match: q.Match(q.Index("hits_by_slug"), slug),
|
||||
},
|
||||
q.If(
|
||||
q.Exists(q.Var("match")),
|
||||
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"), {
|
||||
try {
|
||||
const client = new faunadb.Client({
|
||||
secret: process.env.FAUNADB_SERVER_SECRET,
|
||||
});
|
||||
|
||||
const result = await client.query(
|
||||
q.Let(
|
||||
{
|
||||
match: q.Match(q.Index("hits_by_slug"), slug),
|
||||
},
|
||||
q.If(
|
||||
q.Exists(q.Var("match")),
|
||||
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: {
|
||||
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;
|
||||
|
||||
// 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: slug,
|
||||
hits: hits,
|
||||
pretty_hits: numeral(hits).format("0,0"),
|
||||
pretty_unit: pluralize("hit", hits),
|
||||
}),
|
||||
};
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: JSON.stringify({
|
||||
message: error.message,
|
||||
}),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user