Commit Graph

7 Commits

Author SHA1 Message Date
darkskygit
eb64231899 feat(server): improve logs (#8977) 2024-12-03 06:47:17 +00:00
forehalo
0add8917f9 feat(server): enable share og information for docs (#7794) 2024-09-10 04:03:52 +00:00
forehalo
624f3514fc feat(server): make permission a standalone module (#7880) 2024-08-15 10:01:52 +00:00
forehalo
f2eafc374c feat(server): authenticate user before ws connected (#7777) 2024-08-08 08:30:56 +00:00
forehalo
bd9cb41f8a fix(server): websocket error backward compatibility (#7346) 2024-06-26 12:22:33 +00:00
liuyi
54fc1197ad feat(server): introduce user friendly server errors (#7111) 2024-06-17 11:30:58 +08:00
liuyi
26db1d436d refactor(server): server errors (#5741)
standardize the error raising in both GraphQL Resolvers and Controllers.

Now, All user aware errors should be throwed with `HttpException`'s variants, for example `NotFoundException`.

> Directly throwing `GraphQLError` are forbidden.
The GraphQL errorFormatter will handle it automatically and set `code`, `status` in error extensions.

At the same time, the frontend `GraphQLError` should be imported from `@affine/graphql`, which introduce a better error extensions type.

----
controller example:
```js
@Get('/docs/${id}')
doc() {
  // ...
  // imported from '@nestjs/common'
  throw new NotFoundException('Doc is not found.');
  // ...
}
```
the above will response as:
```
status: 404 Not Found
{
  "message": "Doc is not found.",
  "statusCode": 404,
  "error": "Not Found"
}
```

resolver example:
```js
@Mutation()
invite() {
  // ...
  throw new PayloadTooLargeException('Workspace seats is full.')
  // ...
}
```

the above will response as:
```
status: 200 Ok
{
  "data": null,
  "errors": [
    {
      "message": "Workspace seats is full.",
      "extensions": {
        "code": 404,
        "status": "Not Found"
      }
    }
  ]
}
```

for frontend GraphQLError user-friend, a helper function introduced:

```js
import { findGraphQLError } from '@affine/graphql'

fetch(query)
  .catch(errOrArr => {
    const e = findGraphQLError(errOrArr, e => e.extensions.code === 404)
    if (e) {
      // handle
    }
})
```
2024-01-31 08:43:03 +00:00