All files / src/Logger BaseErrorReport.vue.js

80.64% Statements 25/31
60% Branches 6/10
73.33% Functions 11/15
80.64% Lines 25/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129                        1x             1x                     7x                   6x     7x 7x 14x   7x   7x     7x   7x 7x                 15x 10x   5x 5x                         15x 15x             1x 1x             15x   15x                               15x 15x             1x          
// @ts-check
 
import Vue from "vue";
import { get, noop, startsWith, toString } from "lodash";
import logger from "./BaseLogger";
import $ from "jquery";
import Collapsible from "../BaseCollapsible.vue";
import MarkdownViewer from "../plugins/MarkdownViewer.vue";
import Modal from "../BaseModal.vue";
import { BaseStoreMixin } from "../store/utils";
 
 
const options = {
  /** @type  {JQuery<Element> | null} */
  _elt: null,
  /** @type {{(...args: any[]): void}} */
  _errorCB: noop
};
 
const component = /** @type {V.Constructor<typeof options, any>} */ (Vue).extend({
  name: "BaseErrorReport",
  ...options,
  components: { Collapsible, MarkdownViewer, Modal },
  mixins: [ BaseStoreMixin ],
  /**
   * @return {{
   *  errors: Array<BaseLogger.Error>
   * }}
   */
  data() {
    return { errors: [] };
  },
  computed: {
    /**
     * @return {boolean}
     */
    showKeyHints() { return this.getBaseState([ "ui", "showKeyHints" ]); },
    /**
     * @return {boolean}
     */
    muteAlerts() { return this.getBaseState([ "ui", "muteAlerts" ]); }
  },
  mounted() {
    this.$options._elt = $(this.$el);
    this.$options._errorCB = () => {
      this.errors = logger.getErrors();
    };
    logger.on("error", this.$options._errorCB);
    // Load errors already stored (localStorage)
    this.$options._errorCB();
  },
  destroyed() {
    Eif (this.$options._elt) {
      // @ts-ignore: modal is a plugin of bootstrap
      this.$options._elt.modal("dispose");
      logger.removeListener("error", this.$options._errorCB);
    }
  },
  methods: {
    /**
     * @param  {BaseLogger.Error} error
     * @return {Array<string>}
     */
    alertClass(error /*: Logger$Error */) {
      if (error.type === "error") {
        return [ "text-danger" ];
      }
      else Eif (error.type === "warning") {
        return [ "text-warning" ];
      }
      return [];
    },
    toggle() {
      // @ts-ignore: modal is a plugin of bootstrap
      this.$options._elt.modal("toggle");
    },
    /**
     * @param  {BaseLogger.Error} error
     * @return {string}
     */
    errorDetails(error) {
      try {
        return JSON.stringify(get(error, "error"), null, 2);
      }
      catch (e) {
        return "";
      }
    },
    clearErrors() {
      logger.clear();
      this.$options._errorCB(); // Reload
    },
    /**
     * @param  {BaseLogger.Error} error
     * @return {boolean}
     */
    hasBody(error) {
      const contentType = get(error,
        [ "error", "response", "headers", "content-type" ]);
      return startsWith(contentType, "text/html");
    },
    /**
     * @param  {BaseLogger.Error} error
     * @return {boolean}
     */
    errorBody(error) {
      // support axios and superagent
      return get(error, [ "error", "response", "data" ]) ||
        get(error, [ "error", "response", "body" ]);
    },
    /**
     * @param  {BaseLogger.Error} error
     * @return {string}
     */
    errorStack(error /*: Logger$Error */) {
      const stack = get(error, "error.stack");
      return stack ? toString(stack) : "";
    },
    /** @param  {BaseLogger.Error} error */
    errorConsole(error /*: Logger$Error */) {
      console.log(error);
    },
    toggleMuteAlerts() {
      this.commitBase("ui/update", { muteAlerts: !this.muteAlerts });
    }
  }
});
export default component;