Skip to content
This repository was archived by the owner on Jul 10, 2022. It is now read-only.

Commit 7aa3fe9

Browse files
committed
Fix exception handling (BC in some cases)
1 parent d3ea0dd commit 7aa3fe9

8 files changed

+18
-33
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ A minimal Dependency Injection Container (DIC) which implements PSR-11.
3131
Through [NPM](https://www.npmjs.com) as [@chubbyjs/chubbyjs-container][1].
3232

3333
```sh
34-
npm i @chubbyjs/[email protected].8
34+
npm i @chubbyjs/[email protected].9
3535
```
3636

3737
## Usage

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@chubbyjs/chubbyjs-container",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"description": "A simple PSR-11 container implementation.",
55
"keywords": [
66
"chubbyjs",

Diff for: src/Exceptions/ContainerException.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import PsrContainerExceptionInterface from '@chubbyjs/psr-container/dist/ContainerExceptionInterface';
22

3-
class ContainerException implements PsrContainerExceptionInterface {
4-
name: string;
5-
message: string;
6-
stack?: string;
3+
class ContainerException extends Error implements PsrContainerExceptionInterface {
4+
previous?: unknown;
75

8-
public constructor(message: string, stack?: string) {
6+
private constructor(message: string, previous?: unknown) {
7+
super(message);
98
this.name = 'ContainerException';
10-
this.message = message;
11-
this.stack = stack;
9+
this.previous = previous;
1210
}
1311

1412
public static create(id: string, previous: unknown): ContainerException {
15-
return new ContainerException(
16-
`Could not create service with id "${id}"`,
17-
previous instanceof Error ? previous.stack : undefined,
18-
);
13+
return new ContainerException(`Could not create service with id "${id}"`, previous);
1914
}
2015
}
2116

Diff for: src/Exceptions/ExistsException.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import PsrContainerExceptionInterface from '@chubbyjs/psr-container/dist/ContainerExceptionInterface';
22

3-
class ExistsException implements PsrContainerExceptionInterface {
4-
name: string;
5-
message: string;
6-
stack?: string;
7-
3+
class ExistsException extends Error implements PsrContainerExceptionInterface {
84
public static readonly TYPE_FACTORY = 'factory';
95
public static readonly TYPE_PROTOTYPE_FACTORY = 'prototype factory';
106

11-
public constructor(message: string, stack?: string) {
7+
private constructor(message: string) {
8+
super(message);
129
this.name = 'ExistsException';
13-
this.message = message;
14-
this.stack = stack;
1510
}
1611

1712
public static create(id: string, type: string): ExistsException {

Diff for: src/Exceptions/NotFoundException.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import PsrNotFoundExceptionInterface from '@chubbyjs/psr-container/dist/NotFoundExceptionInterface';
22

3-
class NotFoundException implements PsrNotFoundExceptionInterface {
4-
name: string;
5-
message: string;
6-
stack?: string;
7-
8-
public constructor(message: string, stack?: string) {
3+
class NotFoundException extends Error implements PsrNotFoundExceptionInterface {
4+
private constructor(message: string) {
5+
super(message);
96
this.name = 'NotFoundException';
10-
this.message = message;
11-
this.stack = stack;
127
}
138

149
public static create(id: string): NotFoundException {

Diff for: tests/Exceptions/ContainerException.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { expect, test } from '@jest/globals';
22
import ContainerException from '../../src/Exceptions/ContainerException';
33

44
test('create', () => {
5-
const exception = ContainerException.create('id', new Error('test'));
5+
const error = new Error('test');
6+
7+
const exception = ContainerException.create('id', error);
68

79
expect(exception.name).toBe('ContainerException');
810
expect(exception.message).toBe('Could not create service with id "id"');
9-
expect(exception.stack).toMatch(/^Error: test/);
11+
expect(exception.previous).toBe(error);
1012
});

Diff for: tests/Exceptions/ExistsException.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ test('create', () => {
66

77
expect(exception.name).toBe('ExistsException');
88
expect(exception.message).toBe('Factory with id "id" already exists as "prototype factory"');
9-
expect(exception.stack).toBe(undefined);
109
});

Diff for: tests/Exceptions/NotFoundException.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ test('create', () => {
66

77
expect(exception.name).toBe('NotFoundException');
88
expect(exception.message).toBe('There is no service with id "id"');
9-
expect(exception.stack).toBe(undefined);
109
});

0 commit comments

Comments
 (0)