Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(server): server errors #5741

Merged
merged 1 commit into from
Jan 31, 2024
Merged

refactor(server): server errors #5741

merged 1 commit into from
Jan 31, 2024

Conversation

forehalo
Copy link
Member

@forehalo forehalo commented Jan 30, 2024

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:

@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:

@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:

import { findGraphQLError } from '@affine/graphql'

fetch(query)
  .catch(errOrArr => {
    const e = findGraphQLError(errOrArr, e => e.extensions.code === 404)
    if (e) {
      // handle
    }
})

Copy link

graphite-app bot commented Jan 30, 2024

Your org has enabled the Graphite merge queue for merging into canary

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

You can enable merging using labels in your Graphite merge queue settings.

Copy link
Member Author

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.

Copy link

codecov bot commented Jan 30, 2024

Codecov Report

Attention: 57 lines in your changes are missing coverage. Please review.

Comparison is base (72d9cc1) 64.47% compared to head (26db1d4) 64.64%.

Files Patch % Lines
...ackend/server/src/fundamentals/nestjs/exception.ts 36.00% 16 Missing ⚠️
...ges/backend/server/src/plugins/payment/resolver.ts 35.71% 9 Missing ⚠️
packages/frontend/graphql/src/error.ts 0.00% 7 Missing ⚠️
.../server/src/fundamentals/error/payment-required.ts 50.00% 5 Missing ⚠️
...d/server/src/fundamentals/graphql/logger-plugin.ts 85.29% 5 Missing ⚠️
...ckend/server/src/core/workspaces/resolvers/blob.ts 66.66% 4 Missing ⚠️
packages/frontend/workspace-impl/src/cloud/blob.ts 0.00% 4 Missing ⚠️
.../server/src/core/workspaces/resolvers/workspace.ts 40.00% 3 Missing ⚠️
packages/backend/server/src/core/users/resolver.ts 50.00% 2 Missing ⚠️
...ckend/server/src/core/workspaces/resolvers/page.ts 83.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           canary    #5741      +/-   ##
==========================================
+ Coverage   64.47%   64.64%   +0.16%     
==========================================
  Files         339      343       +4     
  Lines       19242    19303      +61     
  Branches     1632     1644      +12     
==========================================
+ Hits        12407    12478      +71     
+ Misses       6619     6609      -10     
  Partials      216      216              
Flag Coverage Δ
server-test 71.01% <73.71%> (+0.22%) ⬆️
unittest 46.17% <0.00%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@forehalo forehalo force-pushed the 61/server-clean-logs branch 2 times, most recently from e4ab38a to d4bb81e Compare January 30, 2024 08:18
@forehalo forehalo force-pushed the 61/server-clean-logs branch 2 times, most recently from a0e2e6e to ea337be Compare January 30, 2024 09:15
@forehalo forehalo force-pushed the 61/server-clean-logs branch 2 times, most recently from 8ee2d61 to f08469a Compare January 30, 2024 09:53
@github-actions github-actions bot added the test Related to test cases label Jan 30, 2024
@forehalo forehalo changed the title refactor(server): server logger refactor(server): server errors Jan 30, 2024
@forehalo forehalo force-pushed the 61/server-clean-logs branch 3 times, most recently from 5f08d1b to e8853d2 Compare January 31, 2024 02:06
Copy link

graphite-app bot commented Jan 31, 2024

Merge activity

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
    }
})
```
@graphite-app graphite-app bot merged commit 26db1d4 into canary Jan 31, 2024
37 of 39 checks passed
@graphite-app graphite-app bot deleted the 61/server-clean-logs branch January 31, 2024 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants