All files / src/custom-element/mixins/components/errorable ErrorableMixin.ts

38.1% Statements 8/21
8.33% Branches 1/12
75% Functions 3/4
38.1% Lines 8/21

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 1002x             2x   2x       8x                       4x   4x   4x                                                                                                                                         2x  
import { errorEvent } from "../../../../error/ErrorHandler";
import { CustomElementStateMetadata } from "../../../interfaces";
 
/**
 * Mixin that handles errors
 * @param Base 
 */
const ErrorableMixin = Base =>
 
    class Errorable extends Base {
 
        static get state() : Record<string, CustomElementStateMetadata> {
 
            return {
 
                error: {
                    value: undefined
                }
            };
        }
 
        renderError() {
 
            const {
                error
            } = this;
 
            Eif (error === undefined) {
 
                return null;
            }
 
            this.dispatchCustomEvent(errorEvent, {
                error: {
                    message: this.getErrorMessage()
                }
            });
 
            // return (
            //     <gcl-overlay>
            //         <gcl-alert
            //             type="error"
            //             message={this.getErrorMessage()}
            //             closable={true}
            //             style={{ maxWidth: '90%' }}
            //             close={() => {
            //                 this.setError(undefined);
            //             }}
            //         />
            //     </gcl-overlay>
            // );
        }
 
        /**
         * Extracts the error message from the error object
         * @returns The error message from the server
         */
        getErrorMessage(): string {
 
            const {
                error
            } = this;
 
            if (error instanceof Error) {
 
                return error.message;
            }
            else { // Try to find the message of error returned by the server
 
                if (error.payload !== undefined) {
 
                    if (typeof error.payload === 'string') {
 
                        return error.payload;
                    }
                    else {
 
                        const payload = JSON.parse(error.payload);
 
                        if (payload.errors !== undefined) {
 
                            return Object.values(payload.errors).join('\n');
                        }
                        else if (payload.title !== undefined) {
 
                            return payload.title;
                        }
                    }
                }
                else {
 
                    return error.statusText;
                }
            }
        }
 
    };
 
export default ErrorableMixin;