Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/decorator.injector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,15 @@ describe('DecoratorInjector', () => {
scopeSpy.mockClear();
});

it('should record exception with sync function', async () => {
it('should record and finish a span when a sync function throws', async () => {
// given
const error = new Error('hello');

@Injectable()
class HelloService {
@Span('hello')
hi() {
throw new Error('hello');
throw error;
}
}

Expand Down Expand Up @@ -220,9 +222,7 @@ describe('DecoratorInjector', () => {
.mockImplementation(() => scope);

// when
expect(() => {
helloService.hi();
}).toThrowError('hello');
expect(() => helloService.hi()).toThrow(error);

// then
expect(
Expand All @@ -237,22 +237,22 @@ describe('DecoratorInjector', () => {
expect(tracer.startSpan).toHaveBeenCalledWith('hello', { childOf: null });
expect(tracer.scope().active).toHaveBeenCalled();
expect(tracer.scope().activate).toHaveBeenCalled();
expect(mockSpan.finish).toHaveBeenCalled();
expect(mockSpan.setTag).toHaveBeenCalledWith('error', new Error('hello'));
expect(mockSpan.setTag).toHaveBeenCalledWith('error', error);
expect(mockSpan.finish).toHaveBeenCalledTimes(1);

startSpanSpy.mockClear();
scopeSpy.mockClear();
});

it('should record exception with async function', async () => {
it('should record and finish a span when an async function rejects', async () => {
// given
const error = new Error('hello');

@Injectable()
class HelloService {
@Span('hello')
async hi() {
return new Promise((_resolve, reject) => {
setTimeout(() => reject(new Error('hello')), 100);
});
throw error;
}
}

Expand Down Expand Up @@ -280,7 +280,7 @@ describe('DecoratorInjector', () => {
.mockImplementation(() => scope);

// when
await expect(helloService.hi()).rejects.toEqual(new Error('hello'));
await expect(helloService.hi()).rejects.toBe(error);

// then
expect(
Expand All @@ -295,8 +295,8 @@ describe('DecoratorInjector', () => {
expect(tracer.startSpan).toHaveBeenCalledWith('hello', { childOf: null });
expect(tracer.scope().active).toHaveBeenCalled();
expect(tracer.scope().activate).toHaveBeenCalled();
expect(mockSpan.finish).toHaveBeenCalled();
expect(mockSpan.setTag).toHaveBeenCalledWith('error', new Error('hello'));
expect(mockSpan.setTag).toHaveBeenCalledWith('error', error);
expect(mockSpan.finish).toHaveBeenCalledTimes(1);

startSpanSpy.mockClear();
scopeSpy.mockClear();
Expand Down
4 changes: 3 additions & 1 deletion src/decorator.injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export class DecoratorInjector implements Injector {
*/
private static recordException(error, span: Span) {
span.setTag('error', error);
throw error;
}

/**
Expand Down Expand Up @@ -185,6 +184,7 @@ export class DecoratorInjector implements Injector {
.apply(this, args)
.catch((error) => {
DecoratorInjector.recordException(error, span);
throw error;
})
.finally(() => span.finish());
} else {
Expand All @@ -195,6 +195,7 @@ export class DecoratorInjector implements Injector {
return result
.catch((error) => {
DecoratorInjector.recordException(error, span);
throw error;
})
.finally(() => span.finish());
}
Expand All @@ -204,6 +205,7 @@ export class DecoratorInjector implements Injector {
} catch (error) {
DecoratorInjector.recordException(error, span);
span.finish();
throw error;
}
}
});
Expand Down