在Vue.js中过滤堆栈跟踪数组

huangapple go评论59阅读模式
英文:

Filtering a stack trace array in vuejs

问题

I am looking for some good ideas on how to filter an array that contains a stack trace. I have a database table that has four columns, one with the stack trace error messages, one that shows the priority of the error, one that shows the date the error was registered and finally a column that displays a custom-made error message, which I have placed on multiple try-blocks around my system.

On the frontend, I am fetching the data with axios and placing it inside an object called errors. Then in my computed properties, I create an array of fields that contain the individual columns from the database and their data. I use the Bootstrap table to output it.

<template>
  <b-container>
    <b-card class="mt-4">
      <h5>{{ $t('events') }}</h5>
      <b-table
        :items="errors"
        :fields="fields"
        :per-page="[5, 10]"
        sort-desc
        primary-key="id"
      />
    </b-card>
  </b-container>
</template>
<script>
import { errorService } from '@/services/error';
import moment from 'moment';

export default {
  components: {
    CommonTable,
    flapper
  },

  data() {
    return {
      errors: null,
    };
  },
  computed: {
    fields() {
      return [
        {
          key: 'priority',
          label: this.$t('errorLogs.priority'),
          sortable: true
        },
        {
          key: 'creationDateTime',
          label: this.$t('creationDateTime'),
          formatter: date => moment(date).locale(this.$i18n.locale).format('L'),
          sortable: true
        },
        {
          key: 'stackTrace',
          label: this.$t('errorLogs.stackTrace'),
          sortable: true
        },
        {
          key: 'errorMessage',
          label: this.$t('message'),
          sortable: true
        },
      ]
    },
  },
  methods: {
    load() {
      errorService.getErrorLogs().then(result => {
        this.errors = result.data
      })
    }
  },
  created() {
    this.load()
  }
};
</script>

It works as it should, but the output for the stack trace takes up way too much space in the table column.

Ideally it should only show

org.springframework.web.method.annotation.MethodArgumentTypeMismatchException

and then if the user wants more detail they can click on the stack trace and get the full version in a pop-up or something.

I am guessing the easiest solution would be to filter the stack trace so that it does not show any text beyond the ":" sign. But how would I implement this in the setup that I currently have?

I am guessing in computed properties I need to add a method to the stackTrace field.

So:

{
  key: 'stackTrace',
  label: this.$t('errorLogs.stackTrace'),
  sortable: true,
  function: this.filteredStackTrace()
},

And then create a new method.

filteredStackTrace() {
  this.errors.stackTrace.filter(some filter...)
}
英文:

I am looking for some good ideas on how to filter an array that contains a stack trace. I have a database table that has four columns, one with the stack trace error messages, one that shows the priority of the error, one that shows the date the error was registered and finally a column that displays an custom made error message, which I have placed on multiple try-blocks around my system.

On the frontend I am fetching the data with axios and placing it inside an object called errors. Then in my computed properties I create an array of fields that contain the individual columns from the database and their data. I use the Bootstrap table to output it.

 &lt;template&gt;
  &lt;b-container&gt;
    &lt;b-card class=&quot;mt-4&quot;&gt;
      &lt;h5&gt;{{ $t(&#39;events&#39;) }}&lt;/h5&gt;
      &lt;b-table
        :items=&quot;errors&quot;
        :fields=&quot;fields&quot;
        :per-page=&quot;[5, 10]&quot;
        sort-desc
        primary-key=&quot;id&quot;
      /&gt;
    &lt;/b-card&gt;
  &lt;/b-container&gt;
&lt;/template&gt;

&lt;script&gt;
  import {errorService} from &#39;@/services/error&#39;;
  import moment from &#39;moment&#39;;

  export default {
    components: {
      CommonTable,
      flapper
    },

    data() {
      return {
        errors: null,
      };
    },
    computed: {
      fields() {
        return [
          {
            key: &#39;priority&#39;,
            label: this.$t(&#39;errorLogs.priority&#39;),
            sortable: true
          },
          {
            key: &#39;creationDateTime&#39;,
            label: this.$t(&#39;creationDateTime&#39;),
            formatter: date =&gt; moment(date).locale(this.$i18n.locale).format(&#39;L&#39;),
            sortable: true
          },
          {
            key: &#39;stackTrace&#39;,
            label: this.$t(&#39;errorLogs.stackTrace&#39;),
            sortable: true
          },
          {
            key: &#39;errorMessage&#39;,
            label: this.$t(&#39;message&#39;),
            sortable: true
          },
        ]
      },
    },
    methods: {
      load(){
        errorService.getErrorLogs().then(result =&gt; {
          this.errors = result.data
        })
      }
    },
    created() {
      this.load()
    }
  };
&lt;/script&gt;

It works as it should, but the output for the stack trace takes up way too much space in the table column.
在Vue.js中过滤堆栈跟踪数组

Ideally it should only show

org.springframework.web.method.annotation.MethodArgumentTypeMismatchException

and then if the user wants more detail they can click on the stack trace and get the full version in a pop up or something.

I am guessing the easiest solution would be to filter the stack trace, so that it does not show any text beyong the : sign.
But how would I implement this in the setup that I currently have?

I am guessing in computed properties I need add a method to the stackTrace field.

So:

      {
        key: &#39;stackTrace&#39;,
        label: this.$t(&#39;errorLogs.stackTrace&#39;),
        sortable: true
        function: this.filteredStackTrace()
      },

And then create a new method.

filteredStackTrace(){
   this.errors.stackTrace.filter(some filter...)
}

答案1

得分: 1

以下是翻译好的部分:

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->
const app = Vue.createApp({
  data() {
    return {
      st: `线程中的异常“main”java.lang.NullPointerException
        在com.example.myproject.Book.java:16中
        在com.example.myproject.Author.java:25中获取Book标题
        在com.example.myproject.Bootstrap.java:14中`,
      expanded: false
    };
  },
  computed: {
    firstLine() {
      return this.st.split('\n')[0]
    },
    allLines() {
      return this.st.split('\n').filter((item, idx) => idx !== 0).toString()
    }
  },
})
app.mount('#demo')

<!-- 语言:lang-html -->
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  {{ firstLine }}
  <button @click="expanded = !expanded">all</button>
  <div v-if="expanded">{{ allLines }}</div>
</div>

<!-- 结束代码片段 -->

希望这对你有所帮助。如果你有其他问题,请随时提出。

英文:

Maybe something like following snippet:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const app = Vue.createApp({
  data() {
    return {
      st: `Exception in thread &quot;main&quot; java.lang.NullPointerException
        at com.example.myproject.Book.getTitle(Book.java:16)
        at com.example.myproject.Author.getBookTitles(Author.java:25)
        at com.example.myproject.Bootstrap.main(Bootstrap.java:14)`,
      expanded: false
    };
  },
  computed: {
    firstLine() {
      return this.st.split(&#39;\n&#39;)[0]
    },
    allLines() {
      return this.st.split(&#39;\n&#39;).filter((item, idx) =&gt; idx !== 0).toString()
    }
  },
})
app.mount(&#39;#demo&#39;)

<!-- language: lang-html -->

&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;demo&quot;&gt;
  {{ firstLine }}
  &lt;button @click=&quot;expanded = !expanded&quot;&gt;all&lt;/button&gt;
  &lt;div v-if=&quot;expanded&quot;&gt;{{ allLines }}&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 22:02:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定