Skip to content

Commit 691e7ca

Browse files
authored
fix: truncate large error messages before printing to customer (#2260)
* fix: truncate large error messages before printing to customer * PR Updates
1 parent b6f4c54 commit 691e7ca

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

.changeset/rude-wasps-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-deployer': patch
3+
---
4+
5+
truncate large error messages before printing to customer

packages/backend-deployer/src/cdk_deployer.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,24 @@ export class CDKDeployer implements BackendDeployer {
199199
// However if the cdk process didn't run or produced no output, then we have nothing to go on with. So we throw
200200
// this error to aid in some debugging.
201201
if (aggregatedStderr.trim()) {
202+
// If the string is more than 65KB, truncate and keep the last portion.
202203
// eslint-disable-next-line amplify-backend-rules/prefer-amplify-errors
203-
throw new Error(aggregatedStderr);
204+
throw new Error(this.truncateString(aggregatedStderr, 65000));
204205
} else {
205206
throw error;
206207
}
207208
}
208209
};
209210

211+
private truncateString = (str: string, size: number) => {
212+
const encoder = new TextEncoder();
213+
const decoder = new TextDecoder();
214+
const encoded = encoder.encode(str);
215+
return encoded.byteLength > size
216+
? '...truncated...' + decoder.decode(encoded.slice(-size))
217+
: str;
218+
};
219+
210220
private getAppCommand = () =>
211221
this.packageManagerController.getCommand([
212222
'tsx',

0 commit comments

Comments
 (0)