ctucx.git: ctucx.things

simple inventory management web-app

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 
<style lang="scss">
</style>

<template>
	<ul v-if="sanitizedErrors && sanitizedErrors.length" class="lpError">
		<li v-for="error in sanitizedErrors">
			{{ error.message }}
		</li>
	</ul>
</template>

<script>

export default {
	name: 'Errors',
	props: ['errors'],
	computed: {
		sanitizedErrors() {
			let errors = this.errors;
			if (!errors) {
				return [];
			}

			if (typeof errors === 'string') {
				return [{ message: errors }];
			}

			if (typeof errors === 'object' && !(errors instanceof Array) && errors.message) {
				return [errors];
			}

			if (typeof errors === 'object' && errors.errors && errors.errors instanceof Array) {
				errors = errors.errors;
			}

			if (typeof errors === 'object' && errors instanceof Array) {
				if (errors.length === 0) {
					return errors;
				}

				const massagedErrors = errors.map((error) => {
					if (typeof error === 'string') {
						return { message: error };
					}

					if (typeof error === 'object' && error.message) {
						return error;
					}
					return false;
				})
					.filter(error => !!error.message);

				if (massagedErrors.length) {
					return massagedErrors;
				}
			}

			return [{ message: 'An unknown error occurred.' }];
		},
	},
};
</script>