fix: handle 401 errors (#6611)

upstream https://github.com/toeverything/blocksuite/pull/6809
This commit is contained in:
pengx17
2024-04-19 03:51:41 +00:00
parent 99bf7c79d1
commit 8c0732ddf1
2 changed files with 22 additions and 5 deletions

View File

@@ -9,7 +9,11 @@ import {
type QueryOptions,
type RequestOptions,
} from '@affine/graphql';
import { GeneralNetworkError, PaymentRequiredError } from '@blocksuite/blocks';
import {
GeneralNetworkError,
PaymentRequiredError,
UnauthorizedError,
} from '@blocksuite/blocks';
type OptionsField<T extends GraphQLQuery> =
RequestOptions<T>['variables'] extends { options: infer U } ? U : never;
@@ -21,10 +25,16 @@ const fetcher = async <Query extends GraphQLQuery>(
return await defaultFetcher<Query>(options);
} catch (_err) {
const error = Array.isArray(_err) ? _err.at(0) : _err;
if (error.extensions?.code === 402) {
throw new PaymentRequiredError();
const code = error.extensions?.code;
switch (code) {
case 401:
throw new UnauthorizedError();
case 402:
throw new PaymentRequiredError();
default:
throw new GeneralNetworkError();
}
throw new GeneralNetworkError();
}
};

View File

@@ -1,4 +1,4 @@
import { openSettingModalAtom } from '@affine/core/atoms';
import { authAtom, openSettingModalAtom } from '@affine/core/atoms';
import { getBaseUrl } from '@affine/graphql';
import { assertExists } from '@blocksuite/global/utils';
import { AIProvider } from '@blocksuite/presets';
@@ -295,4 +295,11 @@ export function setupAIProvider() {
open: true,
});
});
AIProvider.slots.requestLogin.on(() => {
getCurrentStore().set(authAtom, s => ({
...s,
openModal: true,
}));
});
}