update archive error handling

This commit is contained in:
evanpelle
2024-12-17 14:46:29 -08:00
parent 1417808c14
commit 387fdcdef8
2 changed files with 40 additions and 7 deletions
+1 -1
View File
@@ -240,7 +240,7 @@
* better emojis 🏳️🤦‍♂️🖕☮️🫡😡😈🤡 DONE 12/13/2024
* store ips in bigquery table DONE 12/14/2024
* better error logging in server DONE 12/16/2024
* store and archive player cookies
* store and archive player cookies DONE 12/17/2024
* make ips less precise
* send client logs back to server
* seperate server config from client config
+39 -6
View File
@@ -5,7 +5,6 @@ import { BigQuery } from '@google-cloud/bigquery';
const storage = new Storage();
const bigquery = new BigQuery();
export async function archive(gameRecord: GameRecord) {
try {
// Save metadata to BigQuery
@@ -27,19 +26,21 @@ export async function archive(gameRecord: GameRecord) {
})),
};
await bigquery
const [apiResponse] = await bigquery
.dataset('game_archive')
.table('game_results')
.insert([row]);
console.log(`wrote game metadata to BigQuery: ${gameRecord.id}`);
if (gameRecord.turns.length > 0) {
// Players may see this so make sure to clear PII.
// Players may see this so make sure to clear PII
gameRecord.players.forEach(p => {
p.ip = "REDACTED"
p.persistentID = "REDACTED"
})
console.log(`writing game ${gameRecord.id} to gcs`)
});
console.log(`writing game ${gameRecord.id} to gcs`);
const bucket = storage.bucket("openfront-games");
const file = bucket.file(gameRecord.id);
await file.save(JSON.stringify(GameRecordSchema.parse(gameRecord)), {
@@ -47,6 +48,38 @@ export async function archive(gameRecord: GameRecord) {
});
}
} catch (error) {
console.error(`error archiving game record: ${error}`)
try {
console.error(`Error archiving game ${gameRecord.id}:`);
if (Array.isArray(error?.errors)) {
// Handle BigQuery insertion errors which come as an array
error.errors.forEach((err, index) => {
console.error(`${gameRecord.id}: Archive Error ${index + 1}:`, {
reason: err.reason,
message: err.message,
location: err.location,
debugInfo: err.debugInfo
});
});
} else if (error?.code) {
// Handle Google Cloud Storage errors which typically have error codes
console.error(`${gameRecord.id}: Archive: Storage error:`, {
code: error.code,
message: error.message,
stack: error.stack,
details: error.errors
});
} else {
// Handle generic errors
console.error(`${gameRecord.id}: Archive: Unexpected error:`, {
message: error?.message || error,
stack: error?.stack,
name: error?.name,
...(error && typeof error === 'object' ? error : {})
});
}
} catch (error) {
console.log(`error handling archive error: ${error}`)
}
}
}