не может поймать 503 статус в nodejs обработке ошибок - PullRequest
0 голосов
/ 14 апреля 2020

У меня проблема с получением кода состояния ответа об ошибке из API: Вот что я получаю как ошибку при печати:

{
    \
    "message\":\"503 - {\\\"message\\\":\\\"connection error\\\"}\",\"stack\":\"StatusCodeError: 503 - {\\\"message\\\":\\\"connection error\\\"}\\n    at new StatusCodeError (/opt/app-root/node_modules/request-promise-core/lib/errors.js:32:15)\\n    at Request.plumbing.callback (/opt/app-root/node_modules/request-promise-core/lib/plumbing.js:104:33)\\n    at Request.RP$callback [as _callback] (/opt/app-root/node_modules/request-promise-core/lib/plumbing.js:46:31)\\n    at Request.self.callback (/opt/app-root/node_modules/request/request.js:185:22)\\n    at Request.emit (events.js:311:20)\\n    at Request.EventEmitter.emit (domain.js:482:12)\\n    at Request.<anonymous> (/opt/app-root/node_modules/request/request.js:1154:10)\\n    at Request.emit (events.js:311:20)\\n    at Request.EventEmitter.emit (domain.js:482:12)\\n    at IncomingMessage.<anonymous> (/opt/app-root/node_modules/request/request.js:1076:12)\",\"name\":\"StatusCodeError\",\"statusCode\":503,\"error\":{\"message\":\"connection error\"},\"options\":{\"url\":\"https://xxxxx/api/analytics/v1/job/7a349719-5b7c-4785-813e-f4637f9dc5c9/service/observed-data?op=export&format=csv&destination=minio://data-explorer-exports/8cfbfb71bb4495fa1a9577fa49016bb8.csv\",\"headers\":{\"Authorization\":\"Bearer"},\"json\":true,\"method\":\"GET\",\"simple\":true,\"resolveWithFullResponse\":false,\"transform2xxOnly\":false},\"response\":{\"statusCode\":503,\"body\":{\"message\":\"connection error\"},\"headers\":{\"server\":\"envoy\",\"date\":\"Mon, 13 Apr 2020 16:06:37 GMT\",\"content-type\":\"application/json\",\"content-length\":\"31\",\"access-control-allow-origin\":\"*\",\"x-envoy-upstream-service-time\":\"5111\",\"strict-transport-security\":\"max-age=15552000; includeSubDomains\",\"x-frame-options\":\"SAMEORIGIN\",\"connection\":\"close\"},\"request\":{\"uri\":{\"protocol\":\"https:\",\"slashes\":true,\"auth\":null,\"host\":\"xxxxxx\",\"port\":443,\"hostname\":\"xxxx\",\"hash\":null,\"search\":\"?op=export&format=csv&destination=minio://data-explorer-exports/8cfbfb71bb4495fa1a9577fa49016bb8.csv\",\"query\":\"op=export&format=csv&destination=minio://data-explorer-exports/8cfbfb71bb4495fa1a9577fa49016bb8.csv\",\"pathname\":\"/api/analytics/v1/job/7a349719-5b7c-4785-813e-f4637f9dc5c9/service/observed-data\",\"path\":\"/api/analytics/v1/job/7a349719-5b7c-4785-813e-f4637f9dc5c9/service/observed-data?op=export&format=csv&destination=minio://data-explorer-exports/8cfbfb71bb4495fa1a9577fa49016bb8.csv\",\"href\":\"https://xxxxx\"},\"method\":\"GET\",\"headers\":{\"Authorization\":\"Bearer",\"accept\":\"application/json\"}}}}",

, а вот мой код

    this._logger.debug({log: `Calling ATK to creat csv file for search id ${queryId} and atk job id: ${atk_job_id}`});
    try {
      const response = await rp.get({
        url: `${this._url}/job/${atk_job_id}/service/observed-data?op=export&format=csv&destination=minio://${buckerName}/${queryId}.csv`,
        headers: {
          Authorization: authorization,
        },
        json: true,
      });
      return response;
    } catch (err) {
      this._logger.warn({log: `The atk csv file generation failed : ${JSON.stringify(err)}`});
       if (err && err.statusCode === 503) {
        this._logger.warn({log: `The atk csv is still sleeping: ${err.error.statusCode}}`});
      }
    }
  }

Теперь я ожидаю, что мой код перехватит ошибку в this part if (err && err.statusCode === 503) {

, но этого не происходит, и когда я печатаю код err.statusCode, я получаю неопределенное значение, я запутался. Кто-нибудь может помочь?

...