Skip to content

Commit

Permalink
Improve error naming
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktor-obrebski committed Jul 29, 2022
1 parent ce8194c commit c7c6a5b
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 44 deletions.
4 changes: 2 additions & 2 deletions docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Err.of<TError>(value: TError): Err<TError>
```typescript
import { Err } from 'type-safe-errors';

class UserNotFound extends Error {
class UserNotFoundError extends Error {
name = "UserNotFound" as const;
}

Expand Down Expand Up @@ -243,7 +243,7 @@ Err<TErr>.map<unknown>(callback: (value: never) => never): Err<TErr>;
```typescript
import { Ok, Err } from 'type-safe-errors';

class UserNotFound extends Error {
class UserNotFoundError extends Error {
name = "UserNotFound" as const;
}

Expand Down
6 changes: 3 additions & 3 deletions examples/basic-example/pay.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Err, Ok } from 'type-safe-errors';
import { Product, UserCard, MissingPrice, InvalidCVC } from './types';
import { Product, UserCard, MissingPriceError, InvalidCVCError } from './types';

export async function payForProduct(card: UserCard, product: Product) {
// simulate fetching data
await sleep(10);

if (!card.cvc) {
return Err.of(new InvalidCVC());
return Err.of(new InvalidCVCError());
}

if (product.price === null) {
return Err.of(new MissingPrice());
return Err.of(new MissingPriceError());
}

// payment logic
Expand Down
6 changes: 3 additions & 3 deletions examples/basic-example/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { UserCard, MissingPrice, InvalidCVC, Product };
export { UserCard, MissingPriceError, InvalidCVCError, Product };

interface UserCard {
number: string;
Expand All @@ -9,10 +9,10 @@ interface Product {
price: number | null;
}

class InvalidCVC extends Error {
class InvalidCVCError extends Error {
name = 'InvalidCVC' as const;
}

class MissingPrice extends Error {
class MissingPriceError extends Error {
name = 'MissingPrice' as const;
}
8 changes: 4 additions & 4 deletions examples/express/errors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export { InvalidCVC, UknownProduct, MissingCardNumber };
export { InvalidCVCError, UknownProductError, MissingCardNumberError };

class InvalidCVC extends Error {
class InvalidCVCError extends Error {
name = 'InvalidCVC' as const;
}

class UknownProduct extends Error {
class UknownProductError extends Error {
name = 'UknownProduct' as const;
}

class MissingCardNumber extends Error {
class MissingCardNumberError extends Error {
name = 'MissingCardNumber' as const;
}
12 changes: 8 additions & 4 deletions examples/express/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import morgan from 'morgan';
import { Result } from 'type-safe-errors';

import { getProductPrice, payForProduct } from './pay';
import { InvalidCVC, UknownProduct, MissingCardNumber } from './errors';
import {
InvalidCVCError,
UknownProductError,
MissingCardNumberError,
} from './errors';

interface PaymentRequestBody {
cardNumber: string;
Expand All @@ -24,13 +28,13 @@ app.post<{ Body: PaymentRequestBody }>('/payments', async (req, res) => {
.map((successResult) => {
res.status(200).send({ message: successResult });
})
.mapErr(InvalidCVC, () => {
.mapErr(InvalidCVCError, () => {
res.status(422).send({ message: 'Invalid card CVC' });
})
.mapErr(UknownProduct, () => {
.mapErr(UknownProductError, () => {
res.status(404).send({ message: `Product '${productId}' not found` });
})
.mapErr(MissingCardNumber, () => {
.mapErr(MissingCardNumberError, () => {
res.status(400).send({ message: `Invalid card number: '${cardNumber}'` });
})
.promise();
Expand Down
12 changes: 8 additions & 4 deletions examples/express/pay.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { InvalidCVC, MissingCardNumber, UknownProduct } from './errors';
import { Err, Ok } from 'type-safe-errors';
import {
InvalidCVCError,
MissingCardNumberError,
UknownProductError,
} from './errors';

const supportedProductId = '123';
const supportedCVC = '456';
Expand All @@ -15,11 +19,11 @@ async function payForProduct(
await sleep(10);

if (cvc !== supportedCVC) {
return Err.of(new InvalidCVC());
return Err.of(new InvalidCVCError());
}

if (!cardNumber) {
return Err.of(new MissingCardNumber());
return Err.of(new MissingCardNumberError());
}

return Ok.of(`Success. Payed ${productPrice}`);
Expand All @@ -31,7 +35,7 @@ async function getProductPrice(productId: string) {

return productId === supportedProductId
? Ok.of(12.5)
: Err.of(new UknownProduct());
: Err.of(new UknownProductError());
}

function sleep(sleepMs: number) {
Expand Down
8 changes: 4 additions & 4 deletions examples/fastify/errors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export { InvalidCVC, UknownProduct, MissingCardNumber };
export { InvalidCVCError, UknownProductError, MissingCardNumberError };

class InvalidCVC extends Error {
class InvalidCVCError extends Error {
name = 'InvalidCVC' as const;
}

class UknownProduct extends Error {
class UknownProductError extends Error {
name = 'UknownProduct' as const;
}

class MissingCardNumber extends Error {
class MissingCardNumberError extends Error {
name = 'MissingCardNumber' as const;
}
12 changes: 8 additions & 4 deletions examples/fastify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { fastify } from 'fastify';
import { Result } from 'type-safe-errors';

import { getProductPrice, payForProduct } from './pay';
import { InvalidCVC, UknownProduct, MissingCardNumber } from './errors';
import {
InvalidCVCError,
UknownProductError,
MissingCardNumberError,
} from './errors';

interface PaymentRequestBody {
cardNumber: string;
Expand All @@ -20,13 +24,13 @@ app.post<{ Body: PaymentRequestBody }>('/payments', async (req, reply) => {
.map((successResult) => {
reply.status(200).send({ message: successResult });
})
.mapErr(InvalidCVC, () => {
.mapErr(InvalidCVCError, () => {
reply.status(422).send({ message: 'Invalid card CVC' });
})
.mapErr(UknownProduct, () => {
.mapErr(UknownProductError, () => {
reply.status(404).send({ message: `Product '${productId}' not found` });
})
.mapErr(MissingCardNumber, () => {
.mapErr(MissingCardNumberError, () => {
reply
.status(400)
.send({ message: `Invalid card number: '${cardNumber}'` });
Expand Down
12 changes: 8 additions & 4 deletions examples/fastify/pay.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { InvalidCVC, MissingCardNumber, UknownProduct } from './errors';
import { Err, Ok } from 'type-safe-errors';
import {
InvalidCVCError,
MissingCardNumberError,
UknownProductError,
} from './errors';

const supportedProductId = '123';
const supportedCVC = '456';
Expand All @@ -15,11 +19,11 @@ async function payForProduct(
await sleep(10);

if (cvc !== supportedCVC) {
return Err.of(new InvalidCVC());
return Err.of(new InvalidCVCError());
}

if (!cardNumber) {
return Err.of(new MissingCardNumber());
return Err.of(new MissingCardNumberError());
}

return Ok.of(`Success. Payed ${productPrice}`);
Expand All @@ -31,7 +35,7 @@ async function getProductPrice(productId: string) {

return productId === supportedProductId
? Ok.of(12.5)
: Err.of(new UknownProduct());
: Err.of(new UknownProductError());
}

function sleep(sleepMs: number) {
Expand Down
8 changes: 4 additions & 4 deletions examples/nestjs/pay/errors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export { InvalidCVC, UknownProduct, MissingCardNumber };
export { InvalidCVCError, UknownProductError, MissingCardNumberError };

class InvalidCVC extends Error {
class InvalidCVCError extends Error {
name = 'InvalidCVC' as const;
}

class UknownProduct extends Error {
class UknownProductError extends Error {
name = 'UknownProduct' as const;
}

class MissingCardNumber extends Error {
class MissingCardNumberError extends Error {
name = 'MissingCardNumber' as const;
}
12 changes: 8 additions & 4 deletions examples/nestjs/pay/pay.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { Controller, Post, Body, Res } from '@nestjs/common';
import { Response } from 'express';
import { Result } from 'type-safe-errors';

import { InvalidCVC, UknownProduct, MissingCardNumber } from './errors';
import {
InvalidCVCError,
UknownProductError,
MissingCardNumberError,
} from './errors';
import { PayService } from './pay.service';

class PaymentDto {
Expand All @@ -24,13 +28,13 @@ export class PayController {
.map((successResult) => {
res.status(200).send({ message: successResult });
})
.mapErr(InvalidCVC, () => {
.mapErr(InvalidCVCError, () => {
res.status(422).send({ message: 'Invalid card CVC' });
})
.mapErr(UknownProduct, () => {
.mapErr(UknownProductError, () => {
res.status(404).send({ message: `Product '${productId}' not found` });
})
.mapErr(MissingCardNumber, () => {
.mapErr(MissingCardNumberError, () => {
res
.status(400)
.send({ message: `Invalid card number: '${cardNumber}'` });
Expand Down
12 changes: 8 additions & 4 deletions examples/nestjs/pay/pay.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Injectable } from '@nestjs/common';
import { InvalidCVC, UknownProduct, MissingCardNumber } from './errors';
import { Err, Ok } from 'type-safe-errors';
import {
InvalidCVCError,
UknownProductError,
MissingCardNumberError,
} from './errors';

const supportedProductId = '123';
const supportedCVC = '456';
Expand All @@ -12,11 +16,11 @@ export class PayService {
await sleep(10);

if (cvc !== supportedCVC) {
return Err.of(new InvalidCVC());
return Err.of(new InvalidCVCError());
}

if (!cardNumber) {
return Err.of(new MissingCardNumber());
return Err.of(new MissingCardNumberError());
}

return Ok.of(`Success. Payed ${productPrice}`);
Expand All @@ -28,7 +32,7 @@ export class PayService {

return productId === supportedProductId
? Ok.of(12.5)
: Err.of(new UknownProduct());
: Err.of(new UknownProductError());
}
}

Expand Down

0 comments on commit c7c6a5b

Please sign in to comment.