@cubicecho/server-kit 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/_agents/docs/05-graphql.md +1 -0
- package/dist/graphql/logger.d.ts +2 -1
- package/dist/graphql/logger.js +113 -4
- package/package.json +1 -1
|
@@ -20,5 +20,6 @@ const yoga = createYoga({
|
|
|
20
20
|
- `badInput`, `notFound`, `forbidden`, `conflict`, `unauthenticated` and `rateLimited` build the errors a client reads, by `ErrorCode`. Anything else thrown is masked and logged.
|
|
21
21
|
- `parseOrThrow(schema, value)` takes a zod 4 schema and throws `BAD_USER_INPUT` with its first issue.
|
|
22
22
|
- `requireAuth(context)` returns `context.userId`, or throws `UNAUTHENTICATED`.
|
|
23
|
+
- `graphqlLogger` prints a masked error once, with its path, its cause and the stack. A failed query is printed without its parameters or the driver's `detail`.
|
|
23
24
|
- `REQUEST_ID` gives each request an id of the server's own. It is in the log line of a masked error and in the `x-request-id` response header.
|
|
24
25
|
- In tests, alias `graphql` to `node_modules/graphql/index.js`: Vitest otherwise loads its two builds.
|
package/dist/graphql/logger.d.ts
CHANGED
|
@@ -4,7 +4,8 @@ import { Logger } from 'graphql-yoga';
|
|
|
4
4
|
*
|
|
5
5
|
* @remarks
|
|
6
6
|
* The level is fixed, so `LOG_LEVEL` and `DEBUG` in the environment cannot turn on lines that describe requests.
|
|
7
|
-
* An attribute that is not named here is never printed, since operation variables carry sign-in tokens
|
|
7
|
+
* An attribute that is not named here is never printed, since operation variables carry sign-in tokens, and a failed
|
|
8
|
+
* query is printed without its parameters.
|
|
8
9
|
*/
|
|
9
10
|
export declare const graphqlLogger: Logger;
|
|
10
11
|
/** Yoga's `requestId` option: each request gets an id of ours, never one the caller chose. */
|
package/dist/graphql/logger.js
CHANGED
|
@@ -1,6 +1,114 @@
|
|
|
1
1
|
import { GraphQLError } from 'graphql';
|
|
2
2
|
import { Logger } from 'graphql-yoga';
|
|
3
3
|
const TAG = '[graphql]';
|
|
4
|
+
/** A line of a stack that names a place in the code. */
|
|
5
|
+
const FRAME = /^\s+at /;
|
|
6
|
+
/**
|
|
7
|
+
* Carries an error past the logger, which prints what `toJSON` returns. `GraphQLError` has a `toJSON` of its own that
|
|
8
|
+
* drops the cause, so the error would arrive without it.
|
|
9
|
+
*/
|
|
10
|
+
class Carrier {
|
|
11
|
+
error;
|
|
12
|
+
/**
|
|
13
|
+
* Wraps an error.
|
|
14
|
+
*
|
|
15
|
+
* @param error - What the writer is to get.
|
|
16
|
+
*/
|
|
17
|
+
constructor(error) {
|
|
18
|
+
this.error = error;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Hands the error over.
|
|
22
|
+
*
|
|
23
|
+
* @returns The error itself.
|
|
24
|
+
*/
|
|
25
|
+
toJSON() {
|
|
26
|
+
return this.error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Makes a logger, and every child Yoga makes of it, pass errors on whole.
|
|
31
|
+
*
|
|
32
|
+
* @param logger - The logger, changed in place.
|
|
33
|
+
* @returns The same logger.
|
|
34
|
+
*/
|
|
35
|
+
function keepErrors(logger) {
|
|
36
|
+
const { error, child } = logger;
|
|
37
|
+
logger.error = (...args) => {
|
|
38
|
+
const [first, ...rest] = args;
|
|
39
|
+
const carried = isLineAttributes(first) ? { ...first, err: new Carrier(first.err) } : first;
|
|
40
|
+
Reflect.apply(error, logger, [carried, ...rest]);
|
|
41
|
+
};
|
|
42
|
+
logger.child = (...args) => keepErrors(Reflect.apply(child, logger, args));
|
|
43
|
+
return logger;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Says whether a logged value is attributes with an error in them.
|
|
47
|
+
*
|
|
48
|
+
* @param value - What was logged.
|
|
49
|
+
* @returns Whether it has `err`.
|
|
50
|
+
*/
|
|
51
|
+
function isLineAttributes(value) {
|
|
52
|
+
return typeof value === 'object' && value !== null && 'err' in value;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Says whether an error is a query's. It goes by the shape, so that this entry does not import drizzle.
|
|
56
|
+
*
|
|
57
|
+
* @param error - Any error.
|
|
58
|
+
* @returns Whether it carries a query and its parameters.
|
|
59
|
+
*/
|
|
60
|
+
function isQueryError(error) {
|
|
61
|
+
return 'query' in error && 'params' in error && Array.isArray(error.params);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Lists an error and what caused it.
|
|
65
|
+
*
|
|
66
|
+
* @param error - The outermost error.
|
|
67
|
+
* @returns Them, outermost first.
|
|
68
|
+
*/
|
|
69
|
+
function causes(error) {
|
|
70
|
+
const chain = [];
|
|
71
|
+
let next = error;
|
|
72
|
+
while (next instanceof Error && chain.includes(next) === false) {
|
|
73
|
+
chain.push(next);
|
|
74
|
+
next = next.cause;
|
|
75
|
+
}
|
|
76
|
+
return chain;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Describes one error of a chain with a query in it, without any value the query carried. A driver's `detail` is one
|
|
80
|
+
* too ("Key (email)=(…) already exists"), so only its message and code are read.
|
|
81
|
+
*
|
|
82
|
+
* @param error - The error.
|
|
83
|
+
* @returns Its message and code, then where it was thrown.
|
|
84
|
+
*/
|
|
85
|
+
function withoutValues(error) {
|
|
86
|
+
const message = isQueryError(error) ? `Failed query: ${error.query}` : error.message;
|
|
87
|
+
const code = 'code' in error && typeof error.code === 'string' ? ` (${error.code})` : '';
|
|
88
|
+
const { stack = '' } = error;
|
|
89
|
+
// After the message: a value in it can have a line that looks like a frame.
|
|
90
|
+
const start = stack.lastIndexOf(error.message);
|
|
91
|
+
const isRewritten = start === -1;
|
|
92
|
+
const frames = isRewritten
|
|
93
|
+
? []
|
|
94
|
+
: stack
|
|
95
|
+
.slice(start + error.message.length)
|
|
96
|
+
.split('\n')
|
|
97
|
+
.filter((line) => FRAME.test(line));
|
|
98
|
+
return [`${error.name}: ${message}${code}`, ...frames].join('\n');
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Picks what to print for an error.
|
|
102
|
+
*
|
|
103
|
+
* @param error - The error.
|
|
104
|
+
* @returns The error itself, or a description of it when printing it would print a query's parameters.
|
|
105
|
+
*/
|
|
106
|
+
function printable(error) {
|
|
107
|
+
if (error instanceof Error && causes(error).some(isQueryError)) {
|
|
108
|
+
return causes(error).map(withoutValues).join('\nCaused by: ');
|
|
109
|
+
}
|
|
110
|
+
return error;
|
|
111
|
+
}
|
|
4
112
|
/**
|
|
5
113
|
* Picks what to print for a logged error. A masked one prints its path and its cause, which carries the real stack.
|
|
6
114
|
*
|
|
@@ -12,16 +120,17 @@ function describe(error) {
|
|
|
12
120
|
return [];
|
|
13
121
|
}
|
|
14
122
|
const isMasked = error instanceof GraphQLError && error.originalError !== undefined;
|
|
15
|
-
return isMasked ? [error.path?.join('.') ?? '', error.originalError] : [error];
|
|
123
|
+
return isMasked ? [error.path?.join('.') ?? '', printable(error.originalError)] : [printable(error)];
|
|
16
124
|
}
|
|
17
125
|
/**
|
|
18
126
|
* Yoga's logger with the `[graphql]` tag: warnings and errors, each with its request's id.
|
|
19
127
|
*
|
|
20
128
|
* @remarks
|
|
21
129
|
* The level is fixed, so `LOG_LEVEL` and `DEBUG` in the environment cannot turn on lines that describe requests.
|
|
22
|
-
* An attribute that is not named here is never printed, since operation variables carry sign-in tokens
|
|
130
|
+
* An attribute that is not named here is never printed, since operation variables carry sign-in tokens, and a failed
|
|
131
|
+
* query is printed without its parameters.
|
|
23
132
|
*/
|
|
24
|
-
export const graphqlLogger = new Logger({
|
|
133
|
+
export const graphqlLogger = keepErrors(new Logger({
|
|
25
134
|
level: 'warn',
|
|
26
135
|
writers: [
|
|
27
136
|
{
|
|
@@ -33,6 +142,6 @@ export const graphqlLogger = new Logger({
|
|
|
33
142
|
},
|
|
34
143
|
},
|
|
35
144
|
],
|
|
36
|
-
});
|
|
145
|
+
}));
|
|
37
146
|
/** Yoga's `requestId` option: each request gets an id of ours, never one the caller chose. */
|
|
38
147
|
export const REQUEST_ID = Object.freeze({ trustHeader: false });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cubicecho/server-kit",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "The server code every cubicecho GraphQL backend shares: database boot, id columns and the checks that keep a schema to the standard.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Benjamin Van Treese",
|