1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-03 17:26:37 -04:00

add sentry error logging to all serverless functions

This commit is contained in:
2021-06-20 06:48:27 -04:00
parent 363b4edf1c
commit ffd95fec8f
5 changed files with 188 additions and 10 deletions

View File

@ -1,6 +1,7 @@
/// <reference types="./types/hits" />
import * as Sentry from "@sentry/node";
import * as Tracing from "@sentry/tracing"; // eslint-disable-line @typescript-eslint/no-unused-vars
import { VercelRequest, VercelResponse } from "@vercel/node";
import { Client, query as q } from "faunadb";
import numeral from "numeral";
@ -11,7 +12,7 @@ const baseUrl = "https://jarv.is/";
Sentry.init({
dsn: process.env.SENTRY_DSN || "",
environment: process.env.VERCEL_ENV || process.env.NODE_ENV || process.env.SENTRY_ENVIRONMENT || "",
environment: process.env.NODE_ENV || process.env.VERCEL_ENV || process.env.SENTRY_ENVIRONMENT || "",
tracesSampleRate: 1.0,
});
@ -55,6 +56,8 @@ export default async (req: VercelRequest, res: VercelResponse) => {
res.status(200).json(result);
} catch (error) {
console.error(error);
// log error to sentry, give it 2 seconds to finish sending
Sentry.captureException(error);
await Sentry.flush(2000);

View File

@ -1,5 +1,7 @@
/// <reference types="./types/projects" />
import * as Sentry from "@sentry/node";
import * as Tracing from "@sentry/tracing"; // eslint-disable-line @typescript-eslint/no-unused-vars
import { VercelRequest, VercelResponse } from "@vercel/node";
import { escape } from "html-escaper";
import { DateTime } from "luxon";
@ -10,6 +12,12 @@ import { gql } from "graphql-tag";
const username = "jakejarvis";
const endpoint = "https://api.github.com/graphql";
Sentry.init({
dsn: process.env.SENTRY_DSN || "",
environment: process.env.NODE_ENV || process.env.VERCEL_ENV || process.env.SENTRY_ENVIRONMENT || "",
tracesSampleRate: 1.0,
});
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export default async (req: VercelRequest, res: VercelResponse) => {
try {
@ -37,6 +45,10 @@ export default async (req: VercelRequest, res: VercelResponse) => {
} catch (error) {
console.error(error);
// log error to sentry, give it 2 seconds to finish sending
Sentry.captureException(error);
await Sentry.flush(2000);
res.status(400).json({ message: error.message });
}
};

View File

@ -3,6 +3,8 @@
// Fetches my Spotify most-played tracks or currently playing track.
// Heavily inspired by @leerob: https://leerob.io/snippets/spotify
import * as Sentry from "@sentry/node";
import * as Tracing from "@sentry/tracing"; // eslint-disable-line @typescript-eslint/no-unused-vars
import { VercelRequest, VercelResponse } from "@vercel/node";
import fetch from "node-fetch";
import querystring from "querystring";
@ -10,13 +12,20 @@ import querystring from "querystring";
const { SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, SPOTIFY_REFRESH_TOKEN } = process.env;
const basic = Buffer.from(`${SPOTIFY_CLIENT_ID}:${SPOTIFY_CLIENT_SECRET}`).toString("base64");
const TOKEN_ENDPOINT = `https://accounts.spotify.com/api/token`;
// https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow
const TOKEN_ENDPOINT = `https://accounts.spotify.com/api/token`;
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-the-users-currently-playing-track
const NOW_PLAYING_ENDPOINT = `https://api.spotify.com/v1/me/player/currently-playing`;
// https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-users-top-artists-and-tracks
const TOP_TRACKS_ENDPOINT = `https://api.spotify.com/v1/me/top/tracks?time_range=long_term&limit=10`;
Sentry.init({
dsn: process.env.SENTRY_DSN || "",
environment: process.env.NODE_ENV || process.env.VERCEL_ENV || process.env.SENTRY_ENVIRONMENT || "",
tracesSampleRate: 1.0,
});
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export default async (req: VercelRequest, res: VercelResponse) => {
try {
@ -50,6 +59,10 @@ export default async (req: VercelRequest, res: VercelResponse) => {
} catch (error) {
console.error(error);
// log error to sentry, give it 2 seconds to finish sending
Sentry.captureException(error);
await Sentry.flush(2000);
res.status(400).json({ message: error.message });
}
};