Skip to content

Commit 70008f0

Browse files
author
Gerald Baulig
committed
fix(error-handling): more error handling
1 parent 524eba7 commit 70008f0

File tree

4 files changed

+68
-42
lines changed

4 files changed

+68
-42
lines changed

Dockerfile

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ USER node
66
ARG APP_HOME=/home/node/srv
77
WORKDIR $APP_HOME
88

9-
COPY package.json package.json
10-
COPY package-lock.json package-lock.json
11-
12-
RUN npm ci
13-
149
COPY --chown=node:node . .
1510

11+
RUN npm ci
1612
RUN npm run build
1713

1814

@@ -25,15 +21,9 @@ USER node
2521
ARG APP_HOME=/home/node/srv
2622
WORKDIR $APP_HOME
2723

28-
COPY package.json package.json
29-
COPY package-lock.json package-lock.json
30-
31-
COPY --chown=node:node . $APP_HOME
24+
COPY --chown=node:node ./cfg $APP_HOME/cfg
3225
COPY --chown=node:node --from=build $APP_HOME/lib $APP_HOME/lib
3326

3427
EXPOSE 50051
3528

36-
USER root
37-
USER node
38-
39-
CMD [ "npm", "start" ]
29+
CMD [ "node", "./lib/start.cjs" ]

cfg/config.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@
138138
"flushCacheResponse": {
139139
"messageObject": "io.restorecommerce.commandinterface.CommandResponse"
140140
},
141-
142141
"topics": {
143142
"command": {
144143
"topic": "io.restorecommerce.command",
@@ -219,7 +218,7 @@
219218
"address": "localhost:50053"
220219
},
221220
"fulfillment": {
222-
"disabled": true,
221+
"disabled": false,
223222
"address": "localhost:50067",
224223
"createOnSubmit": true,
225224
"cleanupPostSubmit": true,
@@ -231,7 +230,7 @@
231230
}
232231
},
233232
"fulfillment_product": {
234-
"disabled": true,
233+
"disabled": false,
235234
"address": "localhost:50067"
236235
},
237236
"invoice": {

cfg/config_production.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@
6565
"address": "resource-srv:50051"
6666
},
6767
"fulfillment": {
68-
"disabled": true,
68+
"disabled": false,
6969
"address": "fulfillment-srv:50051"
7070
},
7171
"fulfillment_product": {
72-
"disabled": true,
72+
"disabled": false,
7373
"address": "fulfillment-srv:50051"
7474
},
7575
"invoice": {

src/service.ts

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ export class OrderingService
232232
},
233233
NO_ITEM: {
234234
code: 400,
235-
message: 'No item in cart',
235+
message: 'No item in cart!',
236+
},
237+
ITEM_NOT_FOUND: {
238+
code: 404,
239+
message: '{entity} not found!',
236240
},
237241
};
238242

@@ -494,7 +498,7 @@ export class OrderingService
494498
subject?: Subject,
495499
context?: any
496500
): Promise<OrderListResponse> {
497-
const order_ids = [... new Set(ids)];
501+
const order_ids = [...new Set(ids)];
498502

499503
if (order_ids.length > 1000) {
500504
throw this.createOperationStatusCode(
@@ -636,7 +640,13 @@ export class OrderingService
636640
(id) => !!id
637641
)).values()];
638642

639-
if (product_ids.length > 1000) {
643+
if (!product_ids.length) {
644+
throw this.createOperationStatusCode(
645+
'product',
646+
this.operation_status_codes.NO_ITEM,
647+
);
648+
}
649+
else if (product_ids.length > 1000) {
640650
throw this.createOperationStatusCode(
641651
'product',
642652
this.operation_status_codes.LIMIT_EXHAUSTED,
@@ -662,17 +672,23 @@ export class OrderingService
662672
context,
663673
).then(
664674
(response) => {
665-
if (response.operation_status?.code === 200) {
675+
if (response.operation_status?.code !== 200) {
676+
throw response.operation_status;
677+
}
678+
else if (!response.items?.length) {
679+
throw this.createOperationStatusCode(
680+
'products',
681+
this.operation_status_codes.ITEM_NOT_FOUND,
682+
);
683+
}
684+
else {
666685
return response.items!.reduce(
667686
(a, b) => {
668687
a[b.payload?.id!] = b;
669688
return a;
670689
}, {} as ProductMap
671690
);
672691
}
673-
else {
674-
throw response.operation_status;
675-
}
676692
}
677693
);
678694

@@ -733,18 +749,24 @@ export class OrderingService
733749
context
734750
).then(
735751
response => {
736-
if (response.operation_status?.code === 200) {
737-
return response.items?.reduce(
752+
if (response.operation_status?.code !== 200) {
753+
throw response.operation_status;
754+
}
755+
else if (!response.items?.length) {
756+
throw this.createOperationStatusCode(
757+
'taxes',
758+
this.operation_status_codes.ITEM_NOT_FOUND,
759+
);
760+
}
761+
else {
762+
return response.items!.reduce(
738763
(a, b) => {
739764
a[b.payload?.id!] = b.payload!;
740765
return a;
741766
},
742767
{} as RatioedTaxMap
743768
) ?? {};
744769
}
745-
else {
746-
throw response.operation_status;
747-
}
748770
}
749771
);
750772
}
@@ -807,7 +829,7 @@ export class OrderingService
807829
);
808830
}
809831

810-
private get<T>(
832+
private async get<T>(
811833
ids: (string | undefined)[],
812834
service: CRUDClient,
813835
subject?: Subject,
@@ -823,7 +845,7 @@ export class OrderingService
823845
);
824846
}
825847

826-
return service.read(
848+
return await service.read(
827849
{
828850
filters: [{
829851
filters: [
@@ -841,17 +863,23 @@ export class OrderingService
841863
context,
842864
).then(
843865
(response: any) => {
844-
if (response.operation_status?.code === 200) {
866+
if (response.operation_status?.code !== 200) {
867+
throw response.operation_status;
868+
}
869+
else if (!response.items?.length) {
870+
throw this.createOperationStatusCode(
871+
entity,
872+
this.operation_status_codes.ITEM_NOT_FOUND,
873+
);
874+
}
875+
else {
845876
return response.items?.reduce(
846877
(a: any, b: any) => {
847878
a[b.payload?.id] = b;
848879
return a;
849880
}, {} as T
850881
);
851882
}
852-
else {
853-
throw response.operation_status;
854-
}
855883
}
856884
);
857885
}
@@ -892,9 +920,18 @@ export class OrderingService
892920
order_list: OrderList,
893921
subject?: Subject,
894922
context?: any
895-
): Promise<DeepPartial<OrderListResponse>> {
923+
): Promise<OrderListResponse> {
924+
if (!order_list?.items?.length) {
925+
return {
926+
operation_status: this.createOperationStatusCode(
927+
'order',
928+
this.operation_status_codes.NO_ITEM,
929+
)
930+
};
931+
}
932+
896933
const product_map = await this.getProductMap(
897-
order_list.items ?? [],
934+
order_list.items,
898935
subject,
899936
context
900937
);
@@ -1223,7 +1260,7 @@ export class OrderingService
12231260
items,
12241261
total_count: items.length ?? 0,
12251262
operation_status,
1226-
} as OrderListResponse;
1263+
};
12271264
}
12281265

12291266
public async updateState(
@@ -1365,10 +1402,10 @@ export class OrderingService
13651402
}
13661403

13671404
@access_controlled_function({
1368-
action: AuthZAction.READ,
1369-
operation: Operation.whatIsAllowed,
1405+
action: AuthZAction.EXECUTE,
1406+
operation: Operation.isAllowed,
13701407
context: DefaultACSClientContextFactory,
1371-
resource: [{ resource: 'order' }],
1408+
resource: DefaultResourceFactory('execution.evaluateOrders'),
13721409
database: 'arangoDB',
13731410
useCache: true,
13741411
})

0 commit comments

Comments
 (0)