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,
showFriendlyErrorStack: true,
enableAutoPipelining: true,
maxRetriesPerRequest: 0,
maxRetriesPerRequest: 3,
retryStrategy: (times: number) => {
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);
@ -45,11 +46,11 @@ export function createRedisInstance(config = getRedisConfiguration()) {
const redis = new Redis(options);
redis.on("error", (error: unknown) => {
console.warn("[Redis] Error connecting", error);
console.warn("[Redis] ", error);
});
return redis;
} 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>
) {
const redis = createRedisInstance();
if (!redis) {
res
.status(500)
.json({ error: { message: "Internal API is down", code: 50000 } });
return;
}
try {
const channel = req.query.c
? await getChannelEmotes(redis, req.query.c as string)

View file

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

View file

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

View file

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