Skip to content

Commit

Permalink
Fix JSON Parsing Error for Ladder Top Retrieval
Browse files Browse the repository at this point in the history
This pull request addresses a recurring JSON parsing error (“Unexpected token <”) when fetching ladder top data. The code now validates the response before parsing, gracefully handles non-JSON responses, and prevents crashes by returning null if the data is invalid. This ensures that unexpected server responses (such as HTML errors) no longer cause runtime exceptions.
  • Loading branch information
DieterReinert authored Dec 8, 2024
1 parent adbff8b commit d76dcfa
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions server/chat-plugins/seasons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,20 @@ export function generateFormatSchedule() {
}

export async function getLadderTop(format: string) {
try {
const results = await Net(`https://${Config.routes.root}/ladder/?format=${toID(format)}&json`).get();
const reply = JSON.parse(results);
return reply.toplist;
} catch (e) {
Monitor.crashlog(e, "A season ladder request");
return null;
}
try {

Check failure on line 146 in server/chat-plugins/seasons.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Expected indentation of 1 tab but found 4 spaces
const results = await Net(`https://${Config.routes.root}/ladder/?format=${toID(format)}&json`).get();

Check failure on line 147 in server/chat-plugins/seasons.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Expected indentation of 2 tabs but found 8 spaces
let reply;

Check failure on line 148 in server/chat-plugins/seasons.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Expected indentation of 2 tabs but found 8 spaces
try {

Check failure on line 149 in server/chat-plugins/seasons.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Expected indentation of 2 tabs but found 8 spaces
reply = JSON.parse(results);

Check failure on line 150 in server/chat-plugins/seasons.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Expected indentation of 3 tabs but found 12 spaces
} catch (parseError) {

Check failure on line 151 in server/chat-plugins/seasons.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Expected indentation of 2 tabs but found 8 spaces
Monitor.crashlog(parseError, "Invalid JSON response from ladder request");

Check failure on line 152 in server/chat-plugins/seasons.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Expected indentation of 3 tabs but found 12 spaces
return null;

Check failure on line 153 in server/chat-plugins/seasons.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Expected indentation of 3 tabs but found 12 spaces
}

Check failure on line 154 in server/chat-plugins/seasons.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Expected indentation of 2 tabs but found 8 spaces
return reply.toplist;

Check failure on line 155 in server/chat-plugins/seasons.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Expected indentation of 2 tabs but found 8 spaces
} catch (e) {
Monitor.crashlog(e, "A season ladder request");
return null;
}
}

export async function updateBadgeholders() {
Expand Down

0 comments on commit d76dcfa

Please sign in to comment.