ошибка ответа, отправленная из express, не передается в rxjs / ajax правильно - PullRequest
0 голосов
/ 09 июля 2020

Поток запроса выглядит следующим образом

redux-observables epic <==> rxjs/ajax <==> nodejs-express <=(node-fetch)=> backend service

Эпи c

export function someEpic(action$, state$, { api }) { // api is the rxjs/ajax
  return action$
    .pipe(
      ofType(SOME_TYPE),
      ...
      mergeMap((options) => api(options)
        .pipe(
          map(({ response }) => success(response)),
          catchError((response) => {
            console.log('top response: ', response)
            return of(failure(response))
          }),
        )),
      catchError((response) => {
        console.log('bottom response: ', response)
        return of(failure(response))
      }),
    )
}

ajax

import { ajax } from 'rxjs/ajax' // using rxjs6.5
...
export default function api(data) {
  return ajax({
    url,
    method: 'post',
    crossDomain: true,
    body: JSON.stringify({
      ...
    }),
    headers: {
      'Content-Type': 'application/json',
    },
  })
}

Узел- express

const app = express()
...
function call(app) {
  return app.post('/call', cors(...), async (req, res) => {
   
    const fetchRes = await fetch(uri, options)
    const data = await fetchRes.json()

    if (fetchRes.ok) {
      return res.json(data)
    }

    const { code = 500, message = 'Server error' } = data
    return res.status(code).send(message)
  })
}

// Above's data (error response from backend service)
{
  code: 409,
  message: 'Email err:: AlreadyExists',
} 

Ответ, который я получил в Epi c

catchError((response) => {
  console.log('top response: ', response) // this line
  return of(failure(response))
}),

AjaxErrorImpl { 
    message: "ajax error 409",
    name: "AjaxError",
    ...
    xhr: XMLHttpRequest {
        ...
        response: null
        responseText: (...)
        responseType: "json"
        ...
        status: 409
        statusText: "Conflict"
    }
}

Я хочу, чтобы error.message был таким же, как отправленный серверной службой , что составляет Email err:: AlreadyExists, я играл с node-fetch fetch method и rxjs/ajax, но ничего не работает

...