1
1
/* eslint-disable no-use-before-define,object-curly-newline,arrow-body-style */
2
2
3
+ const _ = require ( 'lodash' ) ;
4
+ const CFError = require ( 'cf-errors' ) ;
3
5
const Promise = require ( 'bluebird' ) ;
4
6
const { sdk } = require ( '../../../../logic' ) ;
5
7
@@ -11,21 +13,27 @@ class AnnotationLogic {
11
13
annotations = await sdk . annotations . list ( { entityId, entityType } ) ;
12
14
} catch ( error ) {
13
15
if ( error . statusCode === 404 ) {
14
- throw new Error ( 'Annotations not found for specified entity' ) ;
16
+ throw new CFError ( {
17
+ message : `Annotations not found for specified entity: type = ${ entityType } ; id = ${ entityId } ` ,
18
+ cause : error ,
19
+ } ) ;
15
20
} else {
16
21
throw error ;
17
22
}
18
23
}
19
24
20
- if ( labels && labels . length ) {
21
- annotations = annotations . filter ( annotation => labels . includes ( annotation . key ) ) ;
25
+ if ( _ . isArray ( labels ) && ! _ . isEmpty ( labels ) ) {
26
+ annotations = _ . filter ( annotations , annotation => labels . includes ( annotation . key ) ) ;
22
27
}
23
28
24
29
return annotations ;
25
30
}
26
31
27
32
static createAnnotations ( { entityId, entityType, labels } ) {
28
- if ( ! labels || ! labels . length ) {
33
+ if ( ! _ . isArray ( labels ) ) {
34
+ throw new Error ( 'Labels should be an array' ) ;
35
+ }
36
+ if ( _ . isEmpty ( labels ) ) {
29
37
throw new Error ( 'Labels are required for set command' ) ;
30
38
}
31
39
@@ -34,15 +42,15 @@ class AnnotationLogic {
34
42
}
35
43
36
44
static deleteAnnotations ( { entityId, entityType, labels } ) {
37
- if ( labels && labels . length ) {
45
+ if ( ! _ . isEmpty ( labels ) ) {
38
46
return Promise . map ( labels , key => sdk . annotations . delete ( { entityId, entityType, key } ) ) ;
39
47
}
40
48
41
49
return sdk . annotations . delete ( { entityId, entityType } ) ;
42
50
}
43
51
44
52
static _parseAnnotations ( labels ) {
45
- return labels . map ( ( label ) => {
53
+ return _ . map ( labels , ( label ) => {
46
54
const [ key , value ] = label . split ( '=' ) ;
47
55
return { key, value } ;
48
56
} ) ;
0 commit comments