Passed
Push — master ( 0c064b...50f875 )
by Christian
15:00 queued 10s
created

src/Administration/Resources/app/administration/src/module/sw-extension/service/extension-error.service.js   A

Complexity

Total Complexity 9
Complexity/F 1.29

Size

Lines of Code 43
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 25
mnd 2
bc 2
fnc 7
dl 0
loc 43
rs 10
bpm 0.2857
cpm 1.2857
noi 1
c 0
b 0
f 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A ExtensionErrorService.name 0 3 1
A ExtensionErrorService.handleError 0 6 1
A ExtensionErrorService._getNotification 0 5 2
A ExtensionErrorService._translateRawNotification 0 7 1
A ExtensionErrorService.constructor 0 4 1
A ExtensionErrorService.handleErrorResponse 0 11 3
1
export default class ExtensionErrorService {
2
    static get name() {
3
        return 'extensionErrorService';
4
    }
5
6
    constructor(errorCodes, fallbackError) {
7
        this.errorCodes = errorCodes;
8
        this.fallbackError = fallbackError;
9
    }
10
11
    handleErrorResponse(errorResponse, translator) {
12
        const errors = Shopware.Utils.get(errorResponse, 'response.data.errors', []);
0 ignored issues
show
Bug introduced by
The variable Shopware seems to be never declared. If this is a global, consider adding a /** global: Shopware */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
13
14
        if (!Array.isArray(errors)) {
15
            return [];
16
        }
17
18
        return errors.map((error) => {
19
            return this.handleError(error, translator);
20
        });
21
    }
22
23
    handleError(error, translator) {
24
        return this._translateRawNotification(
25
            this._getNotification(error, translator),
26
            translator
27
        );
28
    }
29
30
    _getNotification(error) {
31
        return this.errorCodes[error.code] ?
32
            this.errorCodes[error.code] :
33
            this.fallbackError;
34
    }
35
36
    _translateRawNotification(notification, translator) {
37
        return {
38
            title: translator.$tc(notification.title),
39
            message: translator.$tc(notification.message),
40
            autoClose: notification.autoClose
41
        };
42
    }
43
}
44