Skip to content

Commit

Permalink
refactor(Telemetry): Normalize AWS request error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed May 19, 2021
1 parent 07d5b9c commit 5a23931
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/aws/request.js
Expand Up @@ -115,6 +115,11 @@ const getServiceInstance = memoize(
}
);

const normalizerPattern = /(?<!^)([A-Z])/g;
const normalizeErrorCodePostfix = (name) => {
return name.replace(normalizerPattern, '_$1').toUpperCase();
};

/** Execute request to AWS service
* @param {Object|string} [service] - Description of the service to call
* @prop [service.name] - Name of the service to call, support subclasses
Expand Down Expand Up @@ -198,13 +203,17 @@ async function awsRequest(service, method, ...args) {
providerError: Object.assign({}, err, { retryable: false }),
});
}
const providerErrorCodeExtension = err.code ? normalizeErrorCodePostfix(err.code) : 'ERROR';
throw Object.assign(
new ServerlessError(
process.env.SLS_DEBUG && err.stack ? `${err.stack}\n${'-'.repeat(100)}` : message,
err.code
`AWS_${normalizeErrorCodePostfix(service.name)}_${normalizeErrorCodePostfix(
method
)}_${providerErrorCodeExtension}`
),
{
providerError: err,
providerErrorCodeExtension,
}
);
}
Expand Down

0 comments on commit 5a23931

Please sign in to comment.