mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-18 18:41:52 +08:00
fix: handle 401 errors (#6611)
upstream https://github.com/toeverything/blocksuite/pull/6809
This commit is contained in:
+14
-4
@@ -9,7 +9,11 @@ import {
|
|||||||
type QueryOptions,
|
type QueryOptions,
|
||||||
type RequestOptions,
|
type RequestOptions,
|
||||||
} from '@affine/graphql';
|
} from '@affine/graphql';
|
||||||
import { GeneralNetworkError, PaymentRequiredError } from '@blocksuite/blocks';
|
import {
|
||||||
|
GeneralNetworkError,
|
||||||
|
PaymentRequiredError,
|
||||||
|
UnauthorizedError,
|
||||||
|
} from '@blocksuite/blocks';
|
||||||
|
|
||||||
type OptionsField<T extends GraphQLQuery> =
|
type OptionsField<T extends GraphQLQuery> =
|
||||||
RequestOptions<T>['variables'] extends { options: infer U } ? U : never;
|
RequestOptions<T>['variables'] extends { options: infer U } ? U : never;
|
||||||
@@ -21,10 +25,16 @@ const fetcher = async <Query extends GraphQLQuery>(
|
|||||||
return await defaultFetcher<Query>(options);
|
return await defaultFetcher<Query>(options);
|
||||||
} catch (_err) {
|
} catch (_err) {
|
||||||
const error = Array.isArray(_err) ? _err.at(0) : _err;
|
const error = Array.isArray(_err) ? _err.at(0) : _err;
|
||||||
if (error.extensions?.code === 402) {
|
const code = error.extensions?.code;
|
||||||
throw new PaymentRequiredError();
|
|
||||||
|
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 { getBaseUrl } from '@affine/graphql';
|
||||||
import { assertExists } from '@blocksuite/global/utils';
|
import { assertExists } from '@blocksuite/global/utils';
|
||||||
import { AIProvider } from '@blocksuite/presets';
|
import { AIProvider } from '@blocksuite/presets';
|
||||||
@@ -295,4 +295,11 @@ export function setupAIProvider() {
|
|||||||
open: true,
|
open: true,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
AIProvider.slots.requestLogin.on(() => {
|
||||||
|
getCurrentStore().set(authAtom, s => ({
|
||||||
|
...s,
|
||||||
|
openModal: true,
|
||||||
|
}));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user