Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

chore: should and request to http error instance (#4317)

Wenzhao Hu7e6e97c5de07
diff --git a/packages/network/src/services/http/implementations/fetch.ts b/packages/network/src/services/http/implementations/fetch.ts
index 4445c31976..0ad5b88c09 100644
--- a/packages/network/src/services/http/implementations/fetch.ts
+++ b/packages/network/src/services/http/implementations/fetch.ts
@@ -18,13 +18,13 @@
/* eslint-disable no-case-declarations */
import type { Subscriber } from 'rxjs';
-import { Observable } from 'rxjs';
import type { HTTPRequest } from '../request';
import type { HTTPEvent, HTTPResponseBody } from '../response';
-import { HTTPProgress, HTTPResponse, HTTPResponseError } from '../response';
+import type { IHTTPImplementation } from './implementation';
+import { Observable } from 'rxjs';
import { HTTPHeaders } from '../headers';
import { HTTPStatusCode } from '../http';
-import type { IHTTPImplementation } from './implementation';
+import { HTTPProgress, HTTPResponse, HTTPResponseError } from '../response';
/**
* An HTTP implementation using Fetch API. This implementation can both run in browser and Node.js.
@@ -38,7 +38,7 @@ export class FetchHTTPImplementation implements IHTTPImplementation {
this._send(request, subscriber, abortController).then(() => {}, (error) => {
subscriber.error(new HTTPResponseError({
error,
-
+ request,
}));
});
@@ -59,6 +59,7 @@ export class FetchHTTPImplementation implements IHTTPImplementation {
response = await fetchPromise;
} catch (error: any) {
subscriber.error(new HTTPResponseError({
+ request,
error,
status: error.status ?? 0,
statusText: error.statusText ?? 'Unknown Error',
@@ -87,6 +88,7 @@ export class FetchHTTPImplementation implements IHTTPImplementation {
}));
} else {
subscriber.error(new HTTPResponseError({
+ request,
error: body,
status,
statusText,
@@ -137,6 +139,7 @@ export class FetchHTTPImplementation implements IHTTPImplementation {
return body;
} catch (error) {
subscriber.error(new HTTPResponseError({
+ request,
error,
status: response.status,
statusText: response.statusText,
diff --git a/packages/network/src/services/http/implementations/xhr.ts b/packages/network/src/services/http/implementations/xhr.ts
index cd01217ac3..bbd1f3d0f5 100644
--- a/packages/network/src/services/http/implementations/xhr.ts
+++ b/packages/network/src/services/http/implementations/xhr.ts
@@ -19,14 +19,14 @@
import type { Nullable } from '@univerjs/core';
import type { Observer } from 'rxjs';
-import { Observable } from 'rxjs';
+import type { HTTPRequest } from '../request';
+import type { HTTPEvent } from '../response';
+import type { IHTTPImplementation } from './implementation';
+import { Observable } from 'rxjs';
import { HTTPHeaders } from '../headers';
import { ErrorStatusCodeLowerBound, HTTPStatusCode, SuccessStatusCodeLowerBound } from '../http';
-import type { HTTPRequest } from '../request';
-import type { HTTPEvent } from '../response';
import { HTTPResponse, HTTPResponseError, ResponseHeader } from '../response';
-import type { IHTTPImplementation } from './implementation';
/**
* An HTTP implementation using XHR. HTTP service provided by this service could only be async (we do not support sync XHR now).
@@ -105,6 +105,7 @@ export class XHRHTTPImplementation implements IHTTPImplementation {
} else {
observer.error(
new HTTPResponseError({
+ request,
error,
headers,
status,
@@ -117,6 +118,7 @@ export class XHRHTTPImplementation implements IHTTPImplementation {
const onErrorHandler = (error: ProgressEvent) => {
const res = new HTTPResponseError({
+ request,
error,
status: xhr.status || 0,
statusText: xhr.statusText || 'Unknown Error',
diff --git a/packages/network/src/services/http/interceptors/__tests__/auth-interceptor.spec.ts b/packages/network/src/services/http/interceptors/__tests__/auth-interceptor.spec.ts
index f287f090e4..40476e2924 100644
--- a/packages/network/src/services/http/interceptors/__tests__/auth-interceptor.spec.ts
+++ b/packages/network/src/services/http/interceptors/__tests__/auth-interceptor.spec.ts
@@ -14,13 +14,13 @@
* limitations under the License.
*/
-import { afterEach, beforeEach, describe, expect, it, vitest } from 'vitest';
import type { Injector } from '@univerjs/core';
-import { HTTPService } from '../../http.service';
+import { afterEach, beforeEach, describe, expect, it, vitest } from 'vitest';
import { createHTTPTestBed, type MockHTTPImplementation } from '../../__testing__/http-testing-utils';
+import { HTTPHeaders } from '../../headers';
+import { HTTPService } from '../../http.service';
import { IHTTPImplementation } from '../../implementations/implementation';
import { __TEST_ONLY_RESET_REQUEST_UID_DO_NOT_USE_IN_PRODUCTION } from '../../request';
-import { HTTPHeaders } from '../../headers';
import { HTTPResponse, HTTPResponseError } from '../../response';
import { AuthInterceptorFactory } from '../auth-interceptor';
@@ -47,6 +47,8 @@ describe('test "HTTPAuthInterceptor"', () => {
function emitError(uid: number, errorCode: number) {
httpImplementation.getHandler(uid).emitError(new HTTPResponseError({
+ // eslint-disable-next-line ts/no-explicit-any
+ request: {} as any,
headers: new HTTPHeaders(),
status: errorCode,
statusText: 'Internal Server Error',
diff --git a/packages/network/src/services/http/interceptors/__tests__/retry-interceptor.spec.ts b/packages/network/src/services/http/interceptors/__tests__/retry-interceptor.spec.ts
index de0fe8ea09..94abb3608e 100644
--- a/packages/network/src/services/http/interceptors/__tests__/retry-interceptor.spec.ts
+++ b/packages/network/src/services/http/interceptors/__tests__/retry-interceptor.spec.ts
@@ -14,16 +14,16 @@
* limitations under the License.
*/
-import { afterEach, beforeEach, describe, expect, it, vitest } from 'vitest';
-
import type { Injector } from '@univerjs/core';
-import { HTTPService } from '../../http.service';
+
+import { afterEach, beforeEach, describe, expect, it, vitest } from 'vitest';
import { createHTTPTestBed, type MockHTTPImplementation } from '../../__testing__/http-testing-utils';
+import { HTTPHeaders } from '../../headers';
+import { HTTPService } from '../../http.service';
import { IHTTPImplementation } from '../../implementations/implementation';
+import { __TEST_ONLY_RESET_REQUEST_UID_DO_NOT_USE_IN_PRODUCTION } from '../../request';
import { HTTPResponse, HTTPResponseError } from '../../response';
-import { HTTPHeaders } from '../../headers';
import { RetryInterceptorFactory } from '../retry-interceptor';
-import { __TEST_ONLY_RESET_REQUEST_UID_DO_NOT_USE_IN_PRODUCTION } from '../../request';
describe('test "HTTPRetryInterceptor"', () => {
let httpService: HTTPService;
@@ -48,6 +48,8 @@ describe('test "HTTPRetryInterceptor"', () => {
function emitError(uid: number) {
httpImplementation.getHandler(uid).emitError(new HTTPResponseError({
+ // eslint-disable-next-line ts/no-explicit-any
+ request: {} as any,
headers: new HTTPHeaders(),
status: 500,
statusText: 'Internal Server Error',
diff --git a/packages/network/src/services/http/interceptors/__tests__/threshold-interceptor.spec.ts b/packages/network/src/services/http/interceptors/__tests__/threshold-interceptor.spec.ts
index c2de906e32..b484af6ecc 100644
--- a/packages/network/src/services/http/interceptors/__tests__/threshold-interceptor.spec.ts
+++ b/packages/network/src/services/http/interceptors/__tests__/threshold-interceptor.spec.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
+import type { Injector } from '@univerjs/core';
import { awaitTime } from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
-import type { Injector } from '@univerjs/core';
import { createHTTPTestBed, type MockHTTPImplementation } from '../../__testing__/http-testing-utils';
import { HTTPHeaders } from '../../headers';
import { HTTPService } from '../../http.service';
@@ -55,6 +55,8 @@ describe('test "HTTPThresholdInterceptor"', () => {
function emitError(uid: number) {
httpImplementation.getHandler(uid).emitError(new HTTPResponseError({
+ // eslint-disable-next-line ts/no-explicit-any
+ request: {} as any,
headers: new HTTPHeaders(),
status: 400,
statusText: 'Request Failed',
diff --git a/packages/network/src/services/http/response.ts b/packages/network/src/services/http/response.ts
index a8d223e53d..bc18ffc77a 100644
--- a/packages/network/src/services/http/response.ts
+++ b/packages/network/src/services/http/response.ts
@@ -15,6 +15,7 @@
*/
import type { HTTPHeaders } from './headers';
+import type { HTTPRequest } from './request';
export type HTTPEvent<T> = HTTPResponse<T> | HTTPProgress;
export enum HTTPEventType {
@@ -98,22 +99,26 @@ export class ResponseHeader {
// #region error
export class HTTPResponseError {
+ readonly request: HTTPRequest;
readonly headers?: HTTPHeaders;
readonly status?: number;
readonly statusText?: string;
readonly error: any;
constructor({
+ request,
headers,
status,
statusText,
error,
}: {
+ request: HTTPRequest;
headers?: HTTPHeaders;
status?: number;
statusText?: string;
error: any;
}) {
+ this.request = request;
this.headers = headers;
this.status = status;
this.statusText = statusText;
diff --git a/packages/sheets-formula/src/services/register-function.service.ts b/packages/sheets-formula/src/services/register-function.service.ts
index fcfdf8ab6f..3257ca5ecc 100644
--- a/packages/sheets-formula/src/services/register-function.service.ts
+++ b/packages/sheets-formula/src/services/register-function.service.ts
@@ -15,10 +15,9 @@
*/
import type { IDisposable, ILocales } from '@univerjs/core';
-import { createIdentifier, Disposable, DisposableCollection, Inject, LocaleService, Optional, toDisposable } from '@univerjs/core';
import type { IFunctionInfo, PrimitiveValueType } from '@univerjs/engine-formula';
+import { createIdentifier, Disposable, DisposableCollection, Inject, LocaleService, Optional, toDisposable } from '@univerjs/core';
import { CustomFunction, FunctionType, IFunctionService } from '@univerjs/engine-formula';
-
import { IDescriptionService } from './description.service';
import { IRemoteRegisterFunctionService } from './remote/remote-register-function.service';