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

Commit

Permalink
Introduce ThundraChaosError propagate it correctly with AWS SDK int…
Browse files Browse the repository at this point in the history
…egration
  • Loading branch information
İbrahim Gürses committed May 27, 2019
1 parent 9a5f192 commit a1a9b39
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/plugins/error/ThundraChaosError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ThundraChaosError extends Error {
constructor(message: string) {
super();
this.message = message;
this.name = 'ThundraChaosError';
Error.captureStackTrace(this, ThundraChaosError);
Object.setPrototypeOf(this, ThundraChaosError.prototype);
}
}

export default ThundraChaosError;
22 changes: 19 additions & 3 deletions src/plugins/integrations/AWSIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ThundraSpan from '../../opentracing/Span';
import * as opentracing from 'opentracing';
import LambdaEventUtils from '../utils/LambdaEventUtils';
import InvocationSupport from '../support/InvocationSupport';
import ThundraChaosError from '../error/ThundraChaosError';

const shimmer = require('shimmer');
const Hook = require('require-in-the-middle');
Expand Down Expand Up @@ -566,25 +567,40 @@ class AWSIntegration implements Integration {
request.on('error', (error: any) => {
if (error && activeSpan) {
activeSpan.setErrorTag(error);
activeSpan.close();
if (error.injectedByThundra) {
activeSpan.close();
}
}
}).on('complete', (response: any) => {
if (response) {
AWSIntegration.injectTraceLink(activeSpan, request, config);
}
if (activeSpan) {
activeSpan.close();
try {
activeSpan.close();
} catch (error) {
if (error instanceof ThundraChaosError) {
request.emit('error', error);
} else {
ThundraLogger.getInstance().error(error);
}
}
}
});

return originalFunction.apply(this, [originalCallback]);
}
} catch (error) {
if (error instanceof ThundraChaosError) {
this.response.error = error;
} else {
ThundraLogger.getInstance().error(error);
}

const originalFunction = integration.wrappedFuncs[wrappedFunctionName];
if (activeSpan) {
activeSpan.close();
}
ThundraLogger.getInstance().error(error);
return originalFunction.apply(this, [callback]);
}
};
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/listeners/ErrorInjectorSpanListener.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ThundraSpanListener from './ThundraSpanListener';
import ThundraSpan from '../../opentracing/Span';
import ThundraChaosError from '../error/ThundraChaosError';

const koalas = require('koalas');

Expand Down Expand Up @@ -63,7 +64,7 @@ class ErrorInjectorSpanListener implements ThundraSpanListener {

private _injectErrorWithCallback(span: ThundraSpan, me: any, callback: () => any): void {
if (this.counter % this.injectCountFreq === 0) {
const error = new Error(this.errorMessage);
const error = new ThundraChaosError(this.errorMessage);
error.name = this.errorType;
span.setErrorTag(error);
if (typeof(callback) === 'function') {
Expand All @@ -76,7 +77,7 @@ class ErrorInjectorSpanListener implements ThundraSpanListener {

private _injectError(span: ThundraSpan, me: any): void {
if (this.counter % this.injectCountFreq === 0) {
const error = new Error(this.errorMessage);
const error = new ThundraChaosError(this.errorMessage);
error.name = this.errorType;
span.setErrorTag(error);
this.counter++;
Expand Down

0 comments on commit a1a9b39

Please sign in to comment.