mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
fix: handle 401 errors (#6611)
upstream https://github.com/toeverything/blocksuite/pull/6809
This commit is contained in:
@@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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,
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user