为什么错误对象没有状态代码和statusMessage?

时间:2018-07-27 13:50:54

标签: angular swagger-2.0 swagger-codegen

这是场景:

服务类是从swagger编辑器生成的,并导入到我的Angular项目中。

存在不同的端点,出于这个问题,我在主要端点之一下方发布。

public getCompanies(pageNumber: string, pageSize: string, sortOrder: string, observe?: 'body', reportProgress?: boolean): Observable<Array<Company>>;
public getCompanies(pageNumber: string, pageSize: string, sortOrder: string, observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<Array<Company>>>;
public getCompanies(pageNumber: string, pageSize: string, sortOrder: string, observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<Array<Company>>>;
public getCompanies(pageNumber: string, pageSize: string, sortOrder: string, observe: any = 'body', reportProgress: boolean = false ): Observable<any> {
    if (pageNumber === null || pageNumber === undefined) {
        throw new Error('Required parameter pageNumber was null or undefined when calling getCompanies.');
    }
    if (pageSize === null || pageSize === undefined) {
        throw new Error('Required parameter pageSize was null or undefined when calling getCompanies.');
    }
    if (sortOrder === null || sortOrder === undefined) {
        throw new Error('Required parameter sortOrder was null or undefined when calling getCompanies.');
    }

    let queryParameters = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
    if (pageNumber !== undefined) {
        queryParameters = queryParameters.set('pageNumber', <any>pageNumber);
    }
    if (pageSize !== undefined) {
        queryParameters = queryParameters.set('pageSize', <any>pageSize);
    }
    if (sortOrder !== undefined) {
        queryParameters = queryParameters.set('sortOrder', <any>sortOrder);
    }

    let headers = this.defaultHeaders;

    // authentication (oauth) required
    if (this.configuration.accessToken) {
        let accessToken = typeof this.configuration.accessToken === 'function'
            ? this.configuration.accessToken()
            : this.configuration.accessToken;
        headers = headers.set('Authorization', 'Bearer ' + accessToken);
    }

    // to determine the Accept header
    let httpHeaderAccepts: string[] = [
        'application/json'
    ];
    let httpHeaderAcceptSelected: string | undefined = this.configuration.selectHeaderAccept(httpHeaderAccepts);
    if (httpHeaderAcceptSelected != undefined) {
        headers = headers.set("Accept", httpHeaderAcceptSelected);
    }

    // to determine the Content-Type header
    let consumes: string[] = [
        '*/*'
    ];

    return this.httpClient.get<Array<Company>>(`${this.basePath}/companies`,
        {
            params: queryParameters,
            withCredentials: this.configuration.withCredentials,
            headers: headers,
            observe: observe,
            reportProgress: reportProgress
        }
    );
}

请注意,以上代码是自动生成的,无法修改。以上服务的订阅方式如下,

this._myservice.getCompanies( page, size, sortOrder ).subscribe(
  (response) => {
    console.log("Success");
  },
  (error) => {
    console.log("Error: " + error.status);
  }
);

这里仅出于测试目的,我正在使用控制台查看状态消息。我已经测试了服务器返回401 Unauthorised的一种情况。 error.status始终包含0。

在浏览器网络窗口中,我看到错误代码为401,但在角度上始终为状态代码0和statusText“未知错误”。为什么会这样?

1 个答案:

答案 0 :(得分:0)

这可能是由于ErrorResponse对象不具有“ status”属性(至少在使用swagger代码生成2.3.1生成代码时)。您是否尝试访问error.code或error.message?