Skip to content

Commit 4438172

Browse files
committed
🔊 (game-server) Log errors in database in collection apierrors
1 parent 24e632b commit 4438172

File tree

6 files changed

+79
-55
lines changed

6 files changed

+79
-55
lines changed

‎api-node/app/app.ts

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ async function listen(port = env.listen.port.api) {
8888
message: err.message,
8989
},
9090
user: ctx.state.user?._id,
91+
meta: {
92+
source: "api-node",
93+
},
9194
});
9295
if (process.env.NODE_ENV !== "production" && !env.silent) {
9396
console.error(err);

‎api-node/app/models/apierror.ts

+3-55
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,6 @@
1-
import mongoose, { Document, Model, Schema, Types } from "mongoose";
1+
import makeSchema from "@lib/schemas/api-error";
2+
import mongoose from "mongoose";
23

3-
export interface ApiErrorDocument extends Document {
4-
error: {
5-
name: string;
6-
message: string;
7-
stack: string[];
8-
};
9-
request: {
10-
url: string;
11-
method: string;
12-
/**
13-
* Stringified content because mongodb doesn't support dots in keys
14-
*/
15-
body: string;
16-
};
17-
meta?: unknown;
18-
user: Types.ObjectId;
19-
updatedAt?: Date;
20-
}
21-
22-
interface ApiErrorModel extends Model<ApiErrorDocument> {
23-
lastUnread(): Promise<ApiErrorDocument[]>;
24-
}
25-
26-
const schema = new Schema<ApiErrorDocument, ApiErrorModel>(
27-
{
28-
error: {
29-
name: String,
30-
message: String,
31-
stack: [String],
32-
},
33-
user: {
34-
type: Schema.Types.ObjectId,
35-
ref: "User",
36-
},
37-
request: {
38-
url: String,
39-
method: String,
40-
body: String,
41-
},
42-
meta: {},
43-
},
44-
{
45-
timestamps: true,
46-
capped: { size: 10 * 1024 * 1024, max: 10 * 1000 },
47-
}
48-
);
49-
50-
schema.static("lastUnread", async function (this: ApiErrorModel) {
51-
return await this.find({ read: false }).sort("-_id").limit(10);
52-
});
53-
54-
schema.index({ user: 1, createdAt: -1 });
55-
56-
const ApiError = mongoose.model("ApiError", schema);
4+
const ApiError = mongoose.model("ApiError", makeSchema());
575

586
export default ApiError;

‎game-server/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The game server needs to have a user with a role that has:
2626
- Read / Write access to the `gamenotifications` collection (`["insert", "find", "remove"]`)
2727
- Read / Write access to the `locks` collection (`["insert", "find", "remove", "update"]`)
2828
- Read access to the `gameinfos` collection, to load the game engines (`["find"]`)
29+
- Write access to the `apierrors` colletion (`["insert"]`)
2930

3031
Due to mongoose (they should use `{authorizedCollections: true, nameOnly: true}`), this is also needed
3132

@@ -35,6 +36,7 @@ For exemaple, in db shell:
3536

3637
```
3738
db.createRole({role: "game-server", privileges: [
39+
{resource: {db: "gaia-project", collection: "apierrors"}, actions: ["insert"]},
3840
{resource: {db: "gaia-project", collection: "chatmessages"}, actions: ["insert"]},
3941
{resource: {db: "gaia-project", collection: "gameinfos"}, actions: ["find"]},
4042
{resource: {db: "gaia-project", collection: "gamenotifications"}, actions: ["insert", "find", "remove", "update"]},
@@ -43,6 +45,20 @@ db.createRole({role: "game-server", privileges: [
4345
{resource: {db: "gaia-project", collection: ""}, actions: ["listCollections"]}
4446
], roles: []});
4547
48+
// or
49+
50+
db.updateRole("game-server", {privileges: [
51+
{resource: {db: "gaia-project", collection: "apierrors"}, actions: ["insert"]},
52+
{resource: {db: "gaia-project", collection: "chatmessages"}, actions: ["insert"]},
53+
{resource: {db: "gaia-project", collection: "gameinfos"}, actions: ["find"]},
54+
{resource: {db: "gaia-project", collection: "gamenotifications"}, actions: ["insert", "find", "remove", "update"]},
55+
{resource: {db: "gaia-project", collection: "games"}, actions: ["find", "update"]},
56+
{resource: {db: "gaia-project", collection: "locks"}, actions: ["insert", "find", "remove", "update"]},
57+
{resource: {db: "gaia-project", collection: ""}, actions: ["listCollections"]}
58+
], roles: []});
59+
60+
// and
61+
4662
db.createUser({
4763
user: "<name>",
4864
pwd: passwordPrompt(), // Or "<cleartext password>"

‎game-server/app/app.ts

+18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import morgan from "koa-morgan";
99
import "./config/db";
1010
/* Configure passport */
1111
import env from "./config/env";
12+
import ApiError from "./models/apierror";
1213
/* Local stuff */
1314
import router from "./routes";
1415

@@ -59,6 +60,23 @@ app.use(async (ctx, next) => {
5960
ctx.status = 500;
6061
ctx.body = { message: "Internal error" };
6162
}
63+
64+
await ApiError.create({
65+
request: {
66+
url: ctx.request.originalUrl,
67+
method: ctx.request.method,
68+
body: JSON.stringify(ctx.request.body),
69+
},
70+
error: {
71+
name: err.name,
72+
stack: err.stack,
73+
message: err.message,
74+
},
75+
user: ctx.state.user?.id,
76+
meta: {
77+
source: "game-server",
78+
},
79+
});
6280
}
6381
});
6482

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import makeSchema from "@lib/schemas/api-error";
2+
import mongoose from "mongoose";
3+
4+
const ApiError = mongoose.model("ApiError", makeSchema());
5+
6+
export default ApiError;

‎site-lib/schemas/api-error.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ApiError } from "@lib/api-error";
2+
import { Document, Model, Schema, Types } from "mongoose";
3+
4+
export interface ApiErrorDocument extends Document, ApiError<Types.ObjectId> {}
5+
6+
const repr = {
7+
error: {
8+
name: String,
9+
message: String,
10+
stack: [String],
11+
},
12+
user: {
13+
type: Schema.Types.ObjectId,
14+
ref: "User",
15+
},
16+
request: {
17+
url: String,
18+
method: String,
19+
body: String,
20+
},
21+
meta: {},
22+
};
23+
24+
export default function makeSchema<U extends Model<ApiErrorDocument> = Model<ApiErrorDocument>>() {
25+
const schema = new Schema<ApiErrorDocument, U>(repr, {
26+
timestamps: true,
27+
capped: { size: 10 * 1024 * 1024, max: 10 * 1000 },
28+
});
29+
30+
schema.index({ user: 1, createdAt: -1 });
31+
32+
return schema;
33+
}

0 commit comments

Comments
 (0)