Skip to content

Commit

Permalink
fix(error-handling): more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Baulig committed Apr 11, 2024
1 parent 524eba7 commit 70008f0
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 42 deletions.
16 changes: 3 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ USER node
ARG APP_HOME=/home/node/srv
WORKDIR $APP_HOME

COPY package.json package.json
COPY package-lock.json package-lock.json

RUN npm ci

COPY --chown=node:node . .

RUN npm ci
RUN npm run build


Expand All @@ -25,15 +21,9 @@ USER node
ARG APP_HOME=/home/node/srv
WORKDIR $APP_HOME

COPY package.json package.json
COPY package-lock.json package-lock.json

COPY --chown=node:node . $APP_HOME
COPY --chown=node:node ./cfg $APP_HOME/cfg
COPY --chown=node:node --from=build $APP_HOME/lib $APP_HOME/lib

EXPOSE 50051

USER root
USER node

CMD [ "npm", "start" ]
CMD [ "node", "./lib/start.cjs" ]
5 changes: 2 additions & 3 deletions cfg/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
"flushCacheResponse": {
"messageObject": "io.restorecommerce.commandinterface.CommandResponse"
},

"topics": {
"command": {
"topic": "io.restorecommerce.command",
Expand Down Expand Up @@ -219,7 +218,7 @@
"address": "localhost:50053"
},
"fulfillment": {
"disabled": true,
"disabled": false,
"address": "localhost:50067",
"createOnSubmit": true,
"cleanupPostSubmit": true,
Expand All @@ -231,7 +230,7 @@
}
},
"fulfillment_product": {
"disabled": true,
"disabled": false,
"address": "localhost:50067"
},
"invoice": {
Expand Down
4 changes: 2 additions & 2 deletions cfg/config_production.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@
"address": "resource-srv:50051"
},
"fulfillment": {
"disabled": true,
"disabled": false,
"address": "fulfillment-srv:50051"
},
"fulfillment_product": {
"disabled": true,
"disabled": false,
"address": "fulfillment-srv:50051"
},
"invoice": {
Expand Down
85 changes: 61 additions & 24 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ export class OrderingService
},
NO_ITEM: {
code: 400,
message: 'No item in cart',
message: 'No item in cart!',
},
ITEM_NOT_FOUND: {
code: 404,
message: '{entity} not found!',
},
};

Expand Down Expand Up @@ -494,7 +498,7 @@ export class OrderingService
subject?: Subject,
context?: any
): Promise<OrderListResponse> {
const order_ids = [... new Set(ids)];
const order_ids = [...new Set(ids)];

if (order_ids.length > 1000) {
throw this.createOperationStatusCode(
Expand Down Expand Up @@ -636,7 +640,13 @@ export class OrderingService
(id) => !!id
)).values()];

if (product_ids.length > 1000) {
if (!product_ids.length) {
throw this.createOperationStatusCode(
'product',
this.operation_status_codes.NO_ITEM,
);
}
else if (product_ids.length > 1000) {
throw this.createOperationStatusCode(
'product',
this.operation_status_codes.LIMIT_EXHAUSTED,
Expand All @@ -662,17 +672,23 @@ export class OrderingService
context,
).then(
(response) => {
if (response.operation_status?.code === 200) {
if (response.operation_status?.code !== 200) {
throw response.operation_status;
}
else if (!response.items?.length) {
throw this.createOperationStatusCode(
'products',
this.operation_status_codes.ITEM_NOT_FOUND,
);
}
else {
return response.items!.reduce(
(a, b) => {
a[b.payload?.id!] = b;
return a;
}, {} as ProductMap
);
}
else {
throw response.operation_status;
}
}
);

Expand Down Expand Up @@ -733,18 +749,24 @@ export class OrderingService
context
).then(
response => {
if (response.operation_status?.code === 200) {
return response.items?.reduce(
if (response.operation_status?.code !== 200) {
throw response.operation_status;
}
else if (!response.items?.length) {
throw this.createOperationStatusCode(
'taxes',
this.operation_status_codes.ITEM_NOT_FOUND,
);
}
else {
return response.items!.reduce(
(a, b) => {
a[b.payload?.id!] = b.payload!;
return a;
},
{} as RatioedTaxMap
) ?? {};
}
else {
throw response.operation_status;
}
}
);
}
Expand Down Expand Up @@ -807,7 +829,7 @@ export class OrderingService
);
}

private get<T>(
private async get<T>(
ids: (string | undefined)[],
service: CRUDClient,
subject?: Subject,
Expand All @@ -823,7 +845,7 @@ export class OrderingService
);
}

return service.read(
return await service.read(
{
filters: [{
filters: [
Expand All @@ -841,17 +863,23 @@ export class OrderingService
context,
).then(
(response: any) => {
if (response.operation_status?.code === 200) {
if (response.operation_status?.code !== 200) {
throw response.operation_status;
}
else if (!response.items?.length) {
throw this.createOperationStatusCode(
entity,
this.operation_status_codes.ITEM_NOT_FOUND,
);
}
else {
return response.items?.reduce(
(a: any, b: any) => {
a[b.payload?.id] = b;
return a;
}, {} as T
);
}
else {
throw response.operation_status;
}
}
);
}
Expand Down Expand Up @@ -892,9 +920,18 @@ export class OrderingService
order_list: OrderList,
subject?: Subject,
context?: any
): Promise<DeepPartial<OrderListResponse>> {
): Promise<OrderListResponse> {
if (!order_list?.items?.length) {
return {
operation_status: this.createOperationStatusCode(
'order',
this.operation_status_codes.NO_ITEM,
)
};
}

const product_map = await this.getProductMap(
order_list.items ?? [],
order_list.items,
subject,
context
);
Expand Down Expand Up @@ -1223,7 +1260,7 @@ export class OrderingService
items,
total_count: items.length ?? 0,
operation_status,
} as OrderListResponse;
};
}

public async updateState(
Expand Down Expand Up @@ -1365,10 +1402,10 @@ export class OrderingService
}

@access_controlled_function({
action: AuthZAction.READ,
operation: Operation.whatIsAllowed,
action: AuthZAction.EXECUTE,
operation: Operation.isAllowed,
context: DefaultACSClientContextFactory,
resource: [{ resource: 'order' }],
resource: DefaultResourceFactory('execution.evaluateOrders'),
database: 'arangoDB',
useCache: true,
})
Expand Down

0 comments on commit 70008f0

Please sign in to comment.