mirror of
https://github.com/strapi/strapi.git
synced 2025-11-15 01:28:07 +00:00
fix diagnostics reporter
This commit is contained in:
parent
08c173c2ab
commit
1f3c24fc8b
@ -201,24 +201,38 @@ The engine includes a diagnostics reporter which can be used to listen for diagn
|
|||||||
|
|
||||||
Here is an example for creating a diagnostics listener:
|
Here is an example for creating a diagnostics listener:
|
||||||
|
|
||||||
```
|
```typescript
|
||||||
// listener function
|
// listener function
|
||||||
const diagnosticListener: DiagnosticListener = (data: GenericDiagnostic) => {
|
const diagnosticListener: DiagnosticListener = (data: GenericDiagnostic) => {
|
||||||
// handle the diagnostics event, for example with custom logging
|
// handle the diagnostics event, for example with custom logging
|
||||||
}
|
};
|
||||||
|
|
||||||
// add a generic listener
|
// add a generic listener
|
||||||
engine.diagnostics.onDiagnostic(diagnosticsListener);
|
engine.diagnostics.onDiagnostic(diagnosticsListener);
|
||||||
// OR
|
|
||||||
engine.diagnostics.addListener('diagnostic', diagnosticListener);
|
|
||||||
|
|
||||||
// add an error listener
|
// add an error listener
|
||||||
engine.diagnostics.addListener('diagnostic.error', diagnosticListener);
|
engine.diagnostics.on('error', diagnosticListener);
|
||||||
|
|
||||||
// add a warning listener
|
// add a warning listener
|
||||||
engine.diagnostics.addListener('diagnostic.warning', diagnosticListener);
|
engine.diagnostics.on('warning', diagnosticListener);
|
||||||
|
```
|
||||||
|
|
||||||
// engine/diagnostics.ts
|
To emit your own diagnostics event:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const event: Diagnostic = {
|
||||||
|
kind: 'error',
|
||||||
|
details: {
|
||||||
|
message: "Your error"
|
||||||
|
createdAt: new Date(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Here is an excerpt of the relevant types used in the previous examples:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// engine/diagnostic.ts
|
||||||
// format of the data sent to the listener
|
// format of the data sent to the listener
|
||||||
export type GenericDiagnostic<K extends DiagnosticKind, T = unknown> = {
|
export type GenericDiagnostic<K extends DiagnosticKind, T = unknown> = {
|
||||||
kind: K;
|
kind: K;
|
||||||
@ -229,6 +243,33 @@ export type GenericDiagnostic<K extends DiagnosticKind, T = unknown> = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type DiagnosticKind = 'error' | 'warning' | 'info';
|
export type DiagnosticKind = 'error' | 'warning' | 'info';
|
||||||
|
|
||||||
|
export type Diagnostic = ErrorDiagnostic | WarningDiagnostic | InfoDiagnostic;
|
||||||
|
|
||||||
|
export type ErrorDiagnosticSeverity = 'fatal' | 'error' | 'silly';
|
||||||
|
|
||||||
|
export type ErrorDiagnostic = GenericDiagnostic<
|
||||||
|
'error',
|
||||||
|
{
|
||||||
|
name: string;
|
||||||
|
severity: ErrorDiagnosticSeverity;
|
||||||
|
error: Error;
|
||||||
|
}
|
||||||
|
>;
|
||||||
|
|
||||||
|
export type WarningDiagnostic = GenericDiagnostic<
|
||||||
|
'warning',
|
||||||
|
{
|
||||||
|
origin?: string;
|
||||||
|
}
|
||||||
|
>;
|
||||||
|
|
||||||
|
export type InfoDiagnostic<T = unknown> = GenericDiagnostic<
|
||||||
|
'info',
|
||||||
|
{
|
||||||
|
params?: T;
|
||||||
|
}
|
||||||
|
>;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Transforms
|
### Transforms
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user