redis error handling fixes

This commit is contained in:
3zachm 2023-01-22 17:25:04 -08:00
parent 45b0b59b99
commit c31db440db
6 changed files with 41 additions and 15 deletions

View file

@ -24,10 +24,11 @@ export function createRedisInstance(config = getRedisConfiguration()) {
lazyConnect: true, lazyConnect: true,
showFriendlyErrorStack: true, showFriendlyErrorStack: true,
enableAutoPipelining: true, enableAutoPipelining: true,
maxRetriesPerRequest: 0, maxRetriesPerRequest: 3,
retryStrategy: (times: number) => { retryStrategy: (times: number) => {
if (times > 3) { if (times > 3) {
throw new Error(`[Redis] Could not connect after ${times} attempts`); console.log(`[Redis] Could not connect after ${times} attempts`);
return undefined;
} }
return Math.min(times * 200, 1000); return Math.min(times * 200, 1000);
@ -45,11 +46,11 @@ export function createRedisInstance(config = getRedisConfiguration()) {
const redis = new Redis(options); const redis = new Redis(options);
redis.on("error", (error: unknown) => { redis.on("error", (error: unknown) => {
console.warn("[Redis] Error connecting", error); console.warn("[Redis] ", error);
}); });
return redis; return redis;
} catch (e) { } catch (e) {
throw new Error(`[Redis] Could not create a Redis instance`); console.log(`[Redis] Could not create a Redis instance`);
} }
} }

View file

@ -11,7 +11,12 @@ export default async function handler(
res: NextApiResponse<Data> res: NextApiResponse<Data>
) { ) {
const redis = createRedisInstance(); const redis = createRedisInstance();
if (!redis) {
res
.status(500)
.json({ error: { message: "Internal API is down", code: 50000 } });
return;
}
try { try {
const channel = req.query.c const channel = req.query.c
? await getChannelEmotes(redis, req.query.c as string) ? await getChannelEmotes(redis, req.query.c as string)

View file

@ -11,6 +11,12 @@ export default async function handler(
res: NextApiResponse<Data> res: NextApiResponse<Data>
) { ) {
const redis = createRedisInstance(); const redis = createRedisInstance();
if (!redis) {
res.status(500).json({
error: { message: "Internal API is down", code: 50200 },
});
return;
}
try { try {
const channel = req.query.c const channel = req.query.c
@ -21,10 +27,8 @@ export default async function handler(
res.status(200).json({ channel, global }); res.status(200).json({ channel, global });
} catch (e) { } catch (e) {
console.log(e); console.log(e);
res res.status(500).json({
.status(500) error: { message: "BTTV or internal API is down", code: 10200 },
.json({ });
error: { message: "BTTV or internal API is down", code: 10200 },
});
} }
} }

View file

@ -16,6 +16,12 @@ export default async function handler(
const sortAsc = req.query.a ? (req.query.a as string) : undefined; const sortAsc = req.query.a ? (req.query.a as string) : undefined;
const redis = createRedisInstance(); const redis = createRedisInstance();
if (!redis) {
res.status(500).json({
error: { message: "Internal API is down", code: 50100 },
});
return;
}
let data = fakeData; let data = fakeData;
// calculate all net worths // calculate all net worths

View file

@ -11,6 +11,12 @@ export default async function handler(
res: NextApiResponse<Data> res: NextApiResponse<Data>
) { ) {
const redis = createRedisInstance(); const redis = createRedisInstance();
if (!redis) {
res.status(500).json({
error: { message: "Internal API is down", code: 50300 },
});
return;
}
try { try {
const channel = req.query.s const channel = req.query.s

View file

@ -11,6 +11,12 @@ export default async function handler(
res: NextApiResponse<Data> res: NextApiResponse<Data>
) { ) {
const redis = createRedisInstance(); const redis = createRedisInstance();
if (!redis) {
res.status(500).json({
error: { message: "Internal API is down", code: 50100 },
});
return;
}
try { try {
const channel = req.query.c const channel = req.query.c
@ -21,10 +27,8 @@ export default async function handler(
res.status(200).json({ channel, global }); res.status(200).json({ channel, global });
} catch (e) { } catch (e) {
console.log(e); console.log(e);
res res.status(500).json({
.status(500) error: { message: "Twitch or internal API is down", code: 10100 },
.json({ });
error: { message: "Twitch or internal API is down", code: 10100 },
});
} }
} }