{"size": 1379, "ext": "js", "max_stars_repo_path": "src/utils/errors_handling.js", "max_stars_repo_name": "Punksolid/simpligps-frontend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/utils/errors_handling.js", "max_issues_repo_name": "Punksolid/simpligps-frontend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/utils/errors_handling.js", "max_forks_repo_name": "Punksolid/simpligps-frontend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.775862069, "max_line_length": 82, "alphanum_fraction": 0.5852066715}
import { Message } from 'element-ui'
import store from '../store'

export function ErrorsHandling(error) {
  if (error.code === 'ECONNABORTED') {
    Message({
      message: 'Try again later',
      type: 'error'
    })
    return Promise.reject(error)
  }
  if (error.response.status === 422) {
    Message({
      message: error.response.data.errors.message,
      type: 'error'
    })

    var composed_message = ''

    for (var [key_errors, errors] of Object.entries(error.response.data.errors)) {
      let errors_explanations = ''
      for (var error_detail of errors) {
        errors_explanations += `<li>${error_detail}</li>`
      }
      composed_message += `
        ${key_errors}
        <ul>
          ${errors_explanations}
        </ul>
      `
    }
    Message({
      dangerouslyUseHTMLString: true,
      message: composed_message,
      type: 'error'
    })
  }
  if (error.response.status === 401) { // Authentication error
    Message({
      message: error.response.data.message,
      type: 'error',
      duration: 5 * 1000
    })
    store.dispatch('LogOut').then(() => {
      location.reload() // 为了重新实例化vue-router对象 避免bug
    })
  }
  if (error.response.status === 500) { // Internal server error
    Message({
      message: error.response.data.message,
      type: 'error',
      duration: 5 * 1000
    })
  }

  return Promise.reject(error)
}
