Skip to content

Commit

Permalink
feat: decorate errors with entity.name
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sa committed Aug 27, 2024
1 parent 896e9b8 commit f25bd91
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 42 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion common/src/lib/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { entity } from '@deepkit/type';

@entity.name('@error/unsupported-state-transition-exception')
@entity.name('@error/UnsupportedStateTransitionException')
export class UnsupportedStateTransitionException extends Error {}
5 changes: 4 additions & 1 deletion customer-service-api/src/lib/replies.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { UUID } from '@deepkit/type';
import { entity, UUID } from '@deepkit/type';

@entity.name('@error/CustomerNotFound')
export class CustomerNotFound extends Error {
constructor(public readonly id: UUID) {
super();
}
}

@entity.name('@error/CustomerVerificationFailed')
export class CustomerVerificationFailed extends Error {
constructor(public readonly id: UUID) {
super();
}
}

@entity.name('@error/CustomerDisabled')
export class CustomerDisabled extends Error {
constructor(public readonly id: UUID) {
super();
Expand Down
2 changes: 1 addition & 1 deletion customer-service/src/customer.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { restate, RestateEventsPublisher } from 'deepkit-restate';
import { cast, Email, float, UUID } from '@deepkit/type';
import { cast, Email, UUID } from '@deepkit/type';

import { Money, PersonName } from '@ftgo/common';
import {
Expand Down
9 changes: 6 additions & 3 deletions kitchen-service-api/src/lib/replies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { UUID } from '@deepkit/type';
import { entity, UUID } from '@deepkit/type';

export class TicketNotFound {
constructor(public readonly ticketId: UUID) {}
@entity.name('@error/TicketNotFound')
export class TicketNotFound extends Error {
constructor(public readonly ticketId: UUID) {
super();
}
}

export class TicketConfirmed {
Expand Down
5 changes: 2 additions & 3 deletions kitchen-service-api/src/lib/services.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RestateService } from 'deepkit-restate';
import { UUID } from '@deepkit/type';

import { TicketCreated, TicketNotFound } from './replies';
import { TicketCreated } from './replies';
import { TicketDetails } from './types';

export interface KitchenServiceHandlers {
Expand All @@ -20,6 +20,5 @@ export interface KitchenServiceHandlers {

export type KitchenServiceApi = RestateService<
'Kitchen',
KitchenServiceHandlers,
[TicketNotFound]
KitchenServiceHandlers
>;
1 change: 1 addition & 0 deletions order-service-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './lib/entities';
export * from './lib/replies';
export * from './lib/sagas';
export * from './lib/services';
export * from './lib/events';
7 changes: 3 additions & 4 deletions order-service-api/src/lib/replies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ import { entity, UUID } from '@deepkit/type';

import { LineItemQuantityChange, OrderRevision } from './entities';

@entity.name('@error/OrderMinimumNotMet')
export class OrderMinimumNotMet extends Error {}

@entity.name('@error/OrderLineItemNotFound')
export class OrderLineItemNotFound extends Error {}

@entity.name('@error/OrderNotFound')
export class OrderNotFound extends Error {
constructor(public readonly orderId: UUID) {
super();
}
}

export class OrderCreatedEvent {
constructor(public readonly orderId: UUID) {}
}

export class OrderApproved {}

export class OrderRejected {}
Expand Down
13 changes: 2 additions & 11 deletions order-service-api/src/lib/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import { UUID } from '@deepkit/type';
import { RestaurantCreatedEvent } from '@ftgo/restaurant-service-api';

import { OrderDetails } from './entities';
import {
OrderApproved,
OrderMinimumNotMet,
OrderNotFound,
OrderRejected,
} from './replies';
import { OrderApproved, OrderRejected } from './replies';

export interface OrderServiceHandlers {
create(orderId: UUID, details: OrderDetails): Promise<UUID>;
Expand All @@ -24,8 +19,4 @@ export interface OrderServiceHandlers {
reviseMenu(event: RestaurantCreatedEvent): Promise<void>;
}

export type OrderServiceApi = RestateService<
'Order',
OrderServiceHandlers,
[OrderNotFound, OrderMinimumNotMet]
>;
export type OrderServiceApi = RestateService<'Order', OrderServiceHandlers>;
11 changes: 2 additions & 9 deletions order-service/src/order.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { restate, RestateEventsPublisher } from 'deepkit-restate';
import { restate } from 'deepkit-restate';
import { UUID } from '@deepkit/type';

import { RestaurantCreatedEvent } from '@ftgo/restaurant-service-api';
import {
OrderApproved,
OrderCreatedEvent,
OrderDetails,
OrderRejected,
OrderServiceApi,
Expand All @@ -15,10 +14,7 @@ import { OrderRepository } from './order.repository';

@restate.service<OrderServiceApi>()
export class OrderService implements OrderServiceHandlers {
constructor(
private readonly order: OrderRepository,
private readonly events: RestateEventsPublisher,
) {}
constructor(private readonly order: OrderRepository) {}

// @ts-expect-error invalid number of arguments
@(restate.event<RestaurantCreatedEvent>().handler())
Expand Down Expand Up @@ -79,9 +75,6 @@ export class OrderService implements OrderServiceHandlers {
restaurantId,
lineItems,
);
await this.events.publish<[OrderCreatedEvent]>([
new OrderCreatedEvent(order.id),
]);
return order.id;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@deepkit/sql": "^1.0.1-alpha.153",
"@deepkit/type": "^1.0.1-alpha.153",
"@restatedev/restate-sdk": "1.2.1",
"deepkit-restate": "0.0.70",
"deepkit-restate": "0.0.71",
"kafkajs": "2.2.4",
"stripe": "^16.8.0",
"tslib": "2.6.3"
Expand Down
3 changes: 3 additions & 0 deletions payment-service-api/src/lib/replies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class PaymentAuthorizationReversed {
constructor(public readonly paymentId: UUID) {}
}

@entity.name('@error/PaymentAuthorizationFailed')
export class PaymentAuthorizationFailed extends Error {
constructor(
public readonly customerId: UUID,
Expand All @@ -17,12 +18,14 @@ export class PaymentAuthorizationFailed extends Error {
}
}

@entity.name('@error/PaymentNotFound')
export class PaymentNotFound extends Error {
constructor(public readonly paymentId: UUID) {
super();
}
}

@entity.name('@error/PaymentCustomerNotFound')
export class PaymentCustomerNotFound extends Error {
constructor(public readonly customerId: UUID) {
super();
Expand Down
11 changes: 3 additions & 8 deletions payment-service-api/src/lib/services.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { RestateService } from 'deepkit-restate';
import { float, UUID } from '@deepkit/type';
import { UUID } from '@deepkit/type';

import { Money } from '@ftgo/common';

import { PaymentCustomer } from './entities';
import {
PaymentAuthorizationFailed,
PaymentAuthorized,
PaymentCustomerNotFound,
} from './replies';
import { PaymentAuthorized } from './replies';

export interface PaymentServiceHandlers {
authorize(
Expand All @@ -22,6 +18,5 @@ export interface PaymentServiceHandlers {

export type PaymentServiceApi = RestateService<
'Payment',
PaymentServiceHandlers,
[PaymentAuthorizationFailed, PaymentCustomerNotFound]
PaymentServiceHandlers
>;

0 comments on commit f25bd91

Please sign in to comment.