Как обрабатывать HTTP-статус в Angular HttpInterceptor?

Как и Марк Адельсбергер (Mark Adelsberger), я думаю, вы хотите использовать pre-commit, чтобы поймать тот факт, что клиенты не находятся на правильной ветке как можно раньше.

Вот пример pre-commit скрипт, чтобы определить, защищена ли ветка или нет.

#!/bin/sh

# Generic function to verify if a word is part of a list of words (space-separated)
contains() {
    # 1 is the element to verify
    local element=$1
    shift
    # All other arguments are part of the list that may contain the element or not
    # so you don't need to quote the arg when calling this function
    local list="$@"

    case $list in
        $element                        |\
        $element[[:blank:]]*            |\
        *[[:blank:]]$element            |\
        *[[:blank:]]$element[[:blank:]]*)
            # The element is contained in the list
            return 0
            ;;
    esac
    # The element is not contained in the list
    return 1
}

# Space-separated list of branches that should not be committed on
protected_branches='devtool master'
current_branch=$(git rev-parse --abbrev-ref HEAD)

contains $current_branch $protected_branches
if [ $? -eq 0 ]; then
    echo The current branch $current_branch is protected! Create your own before committing.
    exit 1
fi

Чтобы решить проблему нажатия на крючки на стороне клиента, существует несколько SO-сообщений, связанных с этим, здесь один . Во всех случаях нет простого способа сделать это.

-2
задан Harun Yılmaz 19 January 2019 в 17:28
поделиться

2 ответа

Вам не нужно наблюдать за ответом на самом деле. Вы можете удалить {наблюдать: «ответ»}. Все, что вам нужно, чтобы обработать это в перехватчике, это добавить catchError в pipe. Чтобы избежать ошибки (если вы хотите, чтобы запрос завершился успешно) , вы должны вернуть Observable чего-то в catchError. Также вы можете уведомить пользователя или выполнить некоторые действия в отношении определенных ошибок, таких как Klodian, показанный выше.

 return next.handle(request).pipe(
 catchError(error => {
   if (error instanceof HttpErrorResponse) {
   // notify user or perfome actions
   }

   return of([]);   // return empty Observable of array 
  }),
  map((event: any) => {
      console.log(event.status);
  return event;
});
0
ответ дан Dmitriy Kavraiskyi 19 January 2019 в 17:28
поделиться

использовать catchError внутри трубы, как показано ниже:

 .pipe(
       catchError(err => { // catch response error from server
            if (err instanceof HttpErrorResponse) {
                  switch ((<HttpErrorResponse>err).status) {
                     case 401: // if is 401 error
                  }
             }

       })
  );
0
ответ дан Klodian shaba 19 January 2019 в 17:28
поделиться
Другие вопросы по тегам:

Похожие вопросы: