v2: SdkError tests + codemod (#2137)

This commit is contained in:
Konstantin Konstantinov 2026-06-10 20:15:15 +03:00 • committed by GitHub
commit 542d5c9586
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 101 additions and 23 deletions

View file

@ -14,7 +14,7 @@ const REEXPORT_WARNINGS: Record<string, string> = {
'Re-exported RequestHandlerExtra was renamed to ServerContext/ClientContext in v2. Update this re-export manually.',
IsomorphicHeaders: 'Re-exported IsomorphicHeaders was removed in v2 (replaced by standard Headers API). Remove this re-export.',
StreamableHTTPError:
'Re-exported StreamableHTTPError was renamed to SdkError in v2 with different constructor. Update this re-export manually.'
'Re-exported StreamableHTTPError was renamed to SdkHttpError in v2 with a different constructor. Update this re-export manually.'
};
export const importPathsTransform: Transform = {

View file

@ -148,13 +148,14 @@ function handleStreamableHTTPError(sourceFile: SourceFile, diagnostics: Diagnost
warning(
sourceFile.getFilePath(),
node.getStartLineNumber(),
'new StreamableHTTPError(statusCode, statusText, body?) → new SdkError(code, message, data?). ' +
'Constructor arguments differ — manual review required. Map HTTP status to SdkErrorCode enum value.'
'new StreamableHTTPError(statusCode, statusText, body?) → new SdkHttpError(code, message, data). ' +
'Constructor arguments differ — manual review required. Map the HTTP status to a SdkErrorCode enum value ' +
'and pass the HTTP status via the data argument, e.g. { status, statusText }.'
)
);
}
renameAllReferences(sourceFile, localName, 'SdkError');
renameAllReferences(sourceFile, localName, 'SdkHttpError');
changesCount++;
foundImport.remove();
@ -164,7 +165,7 @@ function handleStreamableHTTPError(sourceFile: SourceFile, diagnostics: Diagnost
const targetModule = resolveTargetModule(sourceFile, moduleSpec);
const insertIndex = sourceFile.getImportDeclarations().length;
const importsToAdd = hasConstructorCalls ? ['SdkError', 'SdkErrorCode'] : ['SdkError'];
const importsToAdd = hasConstructorCalls ? ['SdkHttpError', 'SdkErrorCode'] : ['SdkHttpError'];
addOrMergeImport(sourceFile, targetModule, importsToAdd, false, insertIndex);
changesCount++;
@ -172,8 +173,10 @@ function handleStreamableHTTPError(sourceFile: SourceFile, diagnostics: Diagnost
warning(
sourceFile.getFilePath(),
line,
'StreamableHTTPError replaced with SdkError. Constructor arguments differ — manual review required. ' +
'HTTP status is now in error.data?.status.'
'StreamableHTTPError replaced with SdkHttpError (a subclass of SdkError). ' +
'HTTP status and status text are now available via error.status and error.statusText. ' +
'Note: unexpected-content-type responses (HTTP 200 with the wrong content type) are thrown as the ' +
'base SdkError, not SdkHttpError, so a catch-all check should use `instanceof SdkError`.'
)
);