无法触发 requestError 拦截器
Cannot trigger requestError interceptor
我想在处理请求之前检查用户的令牌是否有效,我正在使用拦截器进行此操作。
代码如下:
request: (request: RequestMessage): any => {
return new Promise((resolve, reject) => {
console.log('token expires at', this.expiresAt);
if (this.isTokenExpired()) {
console.log('token expired');
throw new Error("{ message: 'Session expired', statusCode: 401 }");
}
if (this.accessToken) {
this.setAuthorizationHeaders(request);
}
resolve(request);
});
},
requestError: (error): any => {
console.log('request error', error);
throw new Error(`yo ${error}`);
}
然而,虽然我在控制台中看到了错误,但它似乎从未经历过 requestError
。
我查看了源代码中 Interceptor 方法的单元测试,但没有发现与我在这里尝试的有太大不同。我做错了什么?
编辑:
添加 HttpClient
配置代码:
const request = this.httpClient
.createRequest(url)
.withBaseUrl(this.baseUrl)
.withHeader('Accept', 'application/json');
// Interceptor ?
if (withInterceptor) {
request.withInterceptor(this.interceptor);
}
// Content type
if (contentType) {
request.withHeader('Content-Type', contentType);
}
// Params
if (params != null) {
request.withParams(params);
}
// Data
if (data != null) {
request.withContent(urlEncode ? UrlUtils.encodeQueryData(data) : data);
}
// Method
switch (method) {
case 'GET':
request.asGet();
break;
case 'POST':
request.asPost();
break;
case 'PUT':
request.asPut();
break;
case 'DELETE':
request.asDelete();
break;
}
以下内容来自官方文档:
requestError acts as a Promise rejection handler during Request creation and request interceptor execution. It will receive the rejection reason, and can either re-throw, or recover by returning a valid Request.
您可以看到,当且仅当在创建请求期间出现错误时,您才输入 requestError
:格式错误 headers、无效配置等
您在这个问题中提出的用法是完全有效的,因此 requestError
启动时不会抛出任何错误。
我想在处理请求之前检查用户的令牌是否有效,我正在使用拦截器进行此操作。
代码如下:
request: (request: RequestMessage): any => {
return new Promise((resolve, reject) => {
console.log('token expires at', this.expiresAt);
if (this.isTokenExpired()) {
console.log('token expired');
throw new Error("{ message: 'Session expired', statusCode: 401 }");
}
if (this.accessToken) {
this.setAuthorizationHeaders(request);
}
resolve(request);
});
},
requestError: (error): any => {
console.log('request error', error);
throw new Error(`yo ${error}`);
}
然而,虽然我在控制台中看到了错误,但它似乎从未经历过 requestError
。
我查看了源代码中 Interceptor 方法的单元测试,但没有发现与我在这里尝试的有太大不同。我做错了什么?
编辑:
添加 HttpClient
配置代码:
const request = this.httpClient
.createRequest(url)
.withBaseUrl(this.baseUrl)
.withHeader('Accept', 'application/json');
// Interceptor ?
if (withInterceptor) {
request.withInterceptor(this.interceptor);
}
// Content type
if (contentType) {
request.withHeader('Content-Type', contentType);
}
// Params
if (params != null) {
request.withParams(params);
}
// Data
if (data != null) {
request.withContent(urlEncode ? UrlUtils.encodeQueryData(data) : data);
}
// Method
switch (method) {
case 'GET':
request.asGet();
break;
case 'POST':
request.asPost();
break;
case 'PUT':
request.asPut();
break;
case 'DELETE':
request.asDelete();
break;
}
以下内容来自官方文档:
requestError acts as a Promise rejection handler during Request creation and request interceptor execution. It will receive the rejection reason, and can either re-throw, or recover by returning a valid Request.
您可以看到,当且仅当在创建请求期间出现错误时,您才输入 requestError
:格式错误 headers、无效配置等
您在这个问题中提出的用法是完全有效的,因此 requestError
启动时不会抛出任何错误。