Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: safely parse errors from Azure DevOps API #22866

Merged
merged 1 commit into from
Jun 19, 2023
Merged
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
22 changes: 14 additions & 8 deletions lib/modules/platform/azure/azure-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { logger } from '../../../logger';
import { streamToString } from '../../../util/streams';
import { getNewBranchName } from '../util';
import * as azureApi from './azure-got-wrapper';
import { WrappedExceptionSchema } from './schema';
import {
getBranchNameWithoutRefsPrefix,
getBranchNameWithoutRefsheadsPrefix,
Expand Down Expand Up @@ -82,18 +83,23 @@ export async function getFile(
if (item?.readable) {
const fileContent = await streamToString(item);
try {
const jTmp = JSON.parse(fileContent);
if (jTmp.typeKey === 'GitItemNotFoundException') {
// file not found
return null;
}
if (jTmp.typeKey === 'GitUnresolvableToCommitException') {
// branch not found
return null;
const result = await WrappedExceptionSchema.safeParseAsync(
JSON.parse(fileContent)
);
if (result.success) {
if (result.data.typeKey === 'GitItemNotFoundException') {
logger.warn(`Unable to find file ${filePath}`);
return null;
}
if (result.data.typeKey === 'GitUnresolvableToCommitException') {
logger.warn(`Unable to find branch ${branchName}`);
return null;
}
}
} catch (error) {
// it 's not a JSON, so I send the content directly with the line under
}

return fileContent;
}
return null; // no file found
Expand Down
17 changes: 17 additions & 0 deletions lib/modules/platform/azure/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { WrappedException } from 'azure-devops-node-api/interfaces/common/VSSInterfaces';
import { z } from 'zod';

export const WrappedExceptionSchema: z.ZodSchema<WrappedException> = z.lazy(
() =>
z.object({
customProperties: z.record(z.any()).optional(),
errorCode: z.number().optional(),
eventId: z.number().optional(),
helpLink: z.string().optional(),
innerException: WrappedExceptionSchema.optional(),
message: z.string().optional(),
stackTrace: z.string().optional(),
typeKey: z.string().optional(),
typeName: z.string().optional(),
})
);