Skip to content

Commit

Permalink
Fix instances of JSON.stringify(error) in packaging tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
satbai committed Feb 4, 2020
1 parent 398bb85 commit b59a667
Show file tree
Hide file tree
Showing 24 changed files with 86 additions and 33 deletions.
7 changes: 6 additions & 1 deletion Tasks/Common/artifacts-common/connectionDataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ async function getServiceUriFromAreaId(areaId: string, accessToken: string): Pro
return serviceUriFromArea.locationUrl;
} catch (error) {
tl.debug(`Failed to obtain the service URI for area ID ${areaId}`);
tl.debug(JSON.stringify(error));
if (error instanceof Error) {
if (error.message) { tl.debug(error.message); }
if (error.stack) { tl.debug(error.stack); }
} else {
tl.debug(error);
}
throw error;
}
}
7 changes: 6 additions & 1 deletion Tasks/Common/artifacts-common/retryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export async function retryOnException<T>(action: () => Promise<T>, maxTries: nu
throw error;
}
tl.debug(`Attempt failed. Number of tries left: ${maxTries}`);
tl.debug(JSON.stringify(error));
if (error instanceof Error) {
if (error.message) { tl.debug(error.message); }
if (error.stack) { tl.debug(error.stack); }
} else {
tl.debug(error);
}
await delay(retryIntervalInMilliseconds);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"loc.messages.NGCommon_SpsNotFound": "Unable to find the '%s' [%s] area. There may be a problem with your Team Foundation Server installation.",
"loc.messages.NGCommon_UnabletoDetectNuGetVersion": "Unknown NuGet version selected.",
"loc.messages.NGCommon_UnableToFindTool": "Unable to find tool %s",
"loc.messages.Warning_SessionCreationFailed": "Could not create provenance session: %s",
"loc.messages.Warning_SessionCreationFailed": "Could not create provenance session:",
"loc.messages.Warning_UpdatingNuGetVersion": "Updating version of NuGet.exe to %s from %s. Behavior changes or breaking changes might occur as NuGet updates to a new version. If this is not desired, uncheck the 'Check for Latest Version' option in the task."
}
9 changes: 5 additions & 4 deletions Tasks/Common/packaging-common/locationUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as tl from 'azure-pipelines-task-lib/task';
import { IRequestOptions } from 'azure-devops-node-api/interfaces/common/VsoBaseInterfaces';

import * as provenance from "./provenance";
import { logError, LogType } from './util';

export enum ProtocolType {
NuGet,
Expand Down Expand Up @@ -146,7 +147,7 @@ export function getWebApiWithProxy(serviceUri: string, accessToken?: string): vs
};
const webApi = new vsts.WebApi(serviceUri, credentialHandler, options);
tl.debug(`Created webApi client for ${serviceUri}; options: ${JSON.stringify(options)}`);
return webApi
return webApi;
}

// This function is to apply retries generically for any unreliable network calls
Expand All @@ -160,7 +161,7 @@ export async function retryOnExceptionHelper<T>(action: () => Promise<T>, maxTri
throw error;
}
tl.debug(`Network call failed. Number of retries left: ${maxTries}`);
if (error) tl.warning(error.toString());
if (error) { logError(error, LogType.warning); }
await delay(retryIntervalInMilliseconds);
}
}
Expand All @@ -177,8 +178,8 @@ interface RegistryLocation {
};

export async function getFeedRegistryUrl(
packagingUrl: string,
registryType: RegistryType,
packagingUrl: string,
registryType: RegistryType,
feedId: string,
project: string,
accessToken?: string,
Expand Down
2 changes: 1 addition & 1 deletion Tasks/Common/packaging-common/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"NGCommon_SpsNotFound": "Unable to find the '%s' [%s] area. There may be a problem with your Team Foundation Server installation.",
"NGCommon_UnabletoDetectNuGetVersion": "Unknown NuGet version selected.",
"NGCommon_UnableToFindTool": "Unable to find tool %s",
"Warning_SessionCreationFailed": "Could not create provenance session: %s",
"Warning_SessionCreationFailed": "Could not create provenance session:",
"Warning_UpdatingNuGetVersion": "Updating version of NuGet.exe to %s from %s. Behavior changes or breaking changes might occur as NuGet updates to a new version. If this is not desired, uncheck the 'Check for Latest Version' option in the task."
}
}
4 changes: 3 additions & 1 deletion Tasks/Common/packaging-common/provenance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as tl from 'azure-pipelines-task-lib';
import * as VsoBaseInterfaces from 'azure-devops-node-api/interfaces/common/VsoBaseInterfaces';
import { ClientVersioningData } from 'azure-devops-node-api/VsoClient';
import vstsClientBases = require('azure-devops-node-api/ClientApiBases');
import { logError, LogType } from './util';

import * as restclient from 'typed-rest-client/RestClient';

Expand Down Expand Up @@ -67,7 +68,8 @@ export class ProvenanceHelper {
const session = await prov.createSession(sessionRequest, protocol, project);
return session.sessionId;
} catch (error) {
tl.warning(tl.loc("Warning_SessionCreationFailed", JSON.stringify(error)));
tl.warning(tl.loc("Warning_SessionCreationFailed"));
logError(error, LogType.warning);
}
}
return feedId;
Expand Down
32 changes: 30 additions & 2 deletions Tasks/Common/packaging-common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ export function getProjectAndFeedIdFromInputParam(inputParam: string): any {
}

export function getProjectAndFeedIdFromInput(feedProject: string): any {
var projectId = null;
var feedId = feedProject;
let projectId = null;
let feedId = feedProject;
if(feedProject && feedProject.includes("/")) {
const feedProjectParts = feedProject.split("/");
projectId = feedProjectParts[0] || null;
Expand All @@ -95,3 +95,31 @@ export function getProjectAndFeedIdFromInput(feedProject: string): any {
projectId: projectId
}
}

export enum LogType {
debug,
warning,
error
}

function log(message: string, logType: LogType) {
if (logType === LogType.warning) {
tl.warning(message);
} else if (logType === LogType.error) {
tl.error(message);
} else {
tl.debug(message);
}
}

/**
* Logs the error instead of throwing.
*/
export function logError(error: any, logType: LogType = LogType.debug) {
if (error instanceof Error) {
if (error.message) { log(error.message, logType); }
if (error.stack) { log(error.stack, LogType.debug); } // Log stack always as debug
} else {
log(`Error: ${error}`, logType);
}
}
4 changes: 2 additions & 2 deletions Tasks/DotNetCoreCLIV2/pushcommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { IExecOptions } from 'azure-pipelines-task-lib/toolrunner';
import { NuGetConfigHelper2 } from 'packaging-common/nuget/NuGetConfigHelper2';
import * as ngRunner from 'packaging-common/nuget/NuGetToolRunner2';
import * as pkgLocationUtils from 'packaging-common/locationUtilities';
import { getProjectAndFeedIdFromInputParam } from 'packaging-common/util';
import { getProjectAndFeedIdFromInputParam, logError } from 'packaging-common/util';

export async function run(): Promise<void> {
let packagingLocation: pkgLocationUtils.PackagingLocation;
try {
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.NuGet);
} catch (error) {
tl.debug('Unable to get packaging URIs, using default collection URI');
tl.debug(JSON.stringify(error));
logError(error);
const collectionUrl = tl.getVariable('System.TeamFoundationCollectionUri');
packagingLocation = {
PackagingUris: [collectionUrl],
Expand Down
4 changes: 2 additions & 2 deletions Tasks/DotNetCoreCLIV2/restorecommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { IExecOptions } from 'azure-pipelines-task-lib/toolrunner';
import * as nutil from 'packaging-common/nuget/Utility';
import * as commandHelper from 'packaging-common/nuget/CommandHelper';
import * as pkgLocationUtils from 'packaging-common/locationUtilities';
import { getProjectAndFeedIdFromInputParam } from 'packaging-common/util';
import { getProjectAndFeedIdFromInputParam, logError } from 'packaging-common/util';

export async function run(): Promise<void> {
let packagingLocation: pkgLocationUtils.PackagingLocation;
try {
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.NuGet);
} catch (error) {
tl.debug('Unable to get packaging URIs, using default collection URI');
tl.debug(JSON.stringify(error));
logError(error);
const collectionUrl = tl.getVariable('System.TeamFoundationCollectionUri');
packagingLocation = {
PackagingUris: [collectionUrl],
Expand Down
3 changes: 2 additions & 1 deletion Tasks/MavenV2/mavenutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs = require('fs');
import * as tl from 'azure-pipelines-task-lib/task';
import * as tr from 'azure-pipelines-task-lib/toolrunner';
import * as pkgLocationUtils from "packaging-common/locationUtilities";
import { logError } from 'packaging-common/util';

import * as url from "url";
import * as xml2js from 'xml2js';
Expand Down Expand Up @@ -200,7 +201,7 @@ async function collectFeedRepositories(pomContents:string): Promise<any> {
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.Maven);
} catch (error) {
tl.debug("Unable to get packaging URIs, using default collection URI");
tl.debug(JSON.stringify(error));
logError(error);
packagingLocation = {
PackagingUris: [collectionUrl],
DefaultPackagingUri: collectionUrl
Expand Down
3 changes: 2 additions & 1 deletion Tasks/MavenV3/mavenutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs = require('fs');
import * as tl from 'azure-pipelines-task-lib/task';
import * as tr from 'azure-pipelines-task-lib/toolrunner';
import * as pkgLocationUtils from "packaging-common/locationUtilities";
import { logError } from 'packaging-common/util';

import * as url from "url";
import * as xml2js from 'xml2js';
Expand Down Expand Up @@ -200,7 +201,7 @@ export async function collectFeedRepositories(pomContents:string): Promise<any>
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.Maven);
} catch (error) {
tl.debug("Unable to get packaging URIs, using default collection URI");
tl.debug(JSON.stringify(error));
logError(error);
packagingLocation = {
PackagingUris: [collectionUrl],
DefaultPackagingUri: collectionUrl};
Expand Down
6 changes: 3 additions & 3 deletions Tasks/NpmAuthenticateV0/npmauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ async function main(): Promise<void> {
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.Npm);
} catch (error) {
tl.debug('Unable to get packaging URIs, using default collection URI');
tl.debug(JSON.stringify(error));
util.logError(error);
const collectionUrl = tl.getVariable('System.TeamFoundationCollectionUri');
packagingLocation = {
PackagingUris: [collectionUrl],
DefaultPackagingUri: collectionUrl
};
}
let LocalNpmRegistries = await npmutil.getLocalNpmRegistries(workingDirectory, packagingLocation.PackagingUris);

let npmrcFile = fs.readFileSync(npmrc, 'utf8').split(os.EOL);
for (let RegistryURLString of npmrcparser.GetRegistries(npmrc, /* saveNormalizedRegistries */ true)) {
let registryURL = URL.parse(RegistryURLString);
let registry: npmregistry.NpmRegistry;
if (endpointRegistries && endpointRegistries.length > 0) {
for (let serviceEndpoint of endpointRegistries) {

if (util.toNerfDart(serviceEndpoint.url) == util.toNerfDart(RegistryURLString)) {
let serviceURL = URL.parse(serviceEndpoint.url);
console.log(tl.loc("AddingEndpointCredentials", registryURL.host));
Expand Down
3 changes: 2 additions & 1 deletion Tasks/NpmV0/npmtask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import tl = require('vsts-task-lib/task');
import trm = require('vsts-task-lib/toolrunner');
var extend = require('util')._extend;
import * as pkgLocationUtils from "packaging-common/locationUtilities";
import { logError } from 'packaging-common/util';

interface EnvironmentDictionary { [key: string]: string; }

Expand Down Expand Up @@ -199,7 +200,7 @@ async function addBuildCredProviderEnv(env: EnvironmentDictionary) : Promise<Env
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.Npm);
} catch (error) {
tl.debug("Unable to get packaging URIs, using default collection URI");
tl.debug(JSON.stringify(error));
logError(error);
const collectionUrl = tl.getVariable("System.TeamFoundationCollectionUri");
packagingLocation = {
PackagingUris: [collectionUrl],
Expand Down
2 changes: 1 addition & 1 deletion Tasks/NpmV1/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function main(): Promise<void> {
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.Npm);
} catch (error) {
tl.debug('Unable to get packaging URIs, using default collection URI');
tl.debug(JSON.stringify(error));
util.logError(error);
const collectionUrl = tl.getVariable('System.TeamFoundationCollectionUri');
packagingLocation = {
PackagingUris: [collectionUrl],
Expand Down
3 changes: 2 additions & 1 deletion Tasks/NuGetCommandV2/nugetcustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as auth from "packaging-common/nuget/Authentication";
import * as ngToolRunner from "packaging-common/nuget/NuGetToolRunner2";
import * as nutil from "packaging-common/nuget/Utility";
import * as tl from "azure-pipelines-task-lib/task";
import { logError } from 'packaging-common/util';

import peParser = require("packaging-common/pe-parser/index");
import * as pkgLocationUtils from "packaging-common/locationUtilities";
Expand All @@ -23,7 +24,7 @@ export async function run(nuGetPath: string): Promise<void> {
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.NuGet);
} catch (error) {
tl.debug("Unable to get packaging URIs, using default collection URI");
tl.debug(JSON.stringify(error));
logError(error);
const collectionUrl = tl.getVariable("System.TeamFoundationCollectionUri");
packagingLocation = {
PackagingUris: [collectionUrl],
Expand Down
3 changes: 2 additions & 1 deletion Tasks/NuGetCommandV2/nugetpublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import INuGetCommandOptions from "packaging-common/nuget/INuGetCommandOptions2";
import * as vstsNuGetPushToolRunner from "./Common/VstsNuGetPushToolRunner";
import * as vstsNuGetPushToolUtilities from "./Common/VstsNuGetPushToolUtilities";
import { getProjectAndFeedIdFromInputParam } from 'packaging-common/util';
import { logError } from 'packaging-common/util';

class PublishOptions implements INuGetCommandOptions {
constructor(
Expand Down Expand Up @@ -41,7 +42,7 @@ export async function run(nuGetPath: string): Promise<void> {
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.NuGet);
} catch (error) {
tl.debug("Unable to get packaging URIs, using default collection URI");
tl.debug(JSON.stringify(error));
logError(error);
const collectionUrl = tl.getVariable("System.TeamFoundationCollectionUri");
packagingLocation = {
PackagingUris: [collectionUrl],
Expand Down
3 changes: 2 additions & 1 deletion Tasks/NuGetCommandV2/nugetrestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as pkgLocationUtils from "packaging-common/locationUtilities";
import * as telemetry from "utility-common/telemetry";
import INuGetCommandOptions from "packaging-common/nuget/INuGetCommandOptions2";
import { getProjectAndFeedIdFromInputParam } from 'packaging-common/util';
import { logError } from 'packaging-common/util';

class RestoreOptions implements INuGetCommandOptions {
constructor(
Expand All @@ -33,7 +34,7 @@ export async function run(nuGetPath: string): Promise<void> {
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.NuGet);
} catch (error) {
tl.debug("Unable to get packaging URIs, using default collection URI");
tl.debug(JSON.stringify(error));
logError(error);
const collectionUrl = tl.getVariable("System.TeamFoundationCollectionUri");
packagingLocation = {
PackagingUris: [collectionUrl],
Expand Down
3 changes: 2 additions & 1 deletion Tasks/NuGetInstallerV0/nugetinstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as ngToolGetter from "packaging-common/nuget/NuGetToolGetter";
import * as ngToolRunner from "packaging-common/nuget/NuGetToolRunner";
import * as nutil from "packaging-common/nuget/Utility";
import * as pkgLocationUtils from "packaging-common/locationUtilities";
import { logError } from 'packaging-common/util';

class RestoreOptions implements INuGetCommandOptions {
constructor(
Expand All @@ -29,7 +30,7 @@ async function main(): Promise<void> {
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.NuGet);
} catch (error) {
tl.debug("Unable to get packaging URIs, using default collection URI");
tl.debug(JSON.stringify(error));
logError(error);
const collectionUrl = tl.getVariable("System.TeamFoundationCollectionUri");
packagingLocation = {
PackagingUris: [collectionUrl],
Expand Down
3 changes: 2 additions & 1 deletion Tasks/NuGetPublisherV0/nugetpublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as ngToolGetter from "packaging-common/nuget/NuGetToolGetter";
import * as ngToolRunner from "packaging-common/nuget/NuGetToolRunner";
import * as nutil from "packaging-common/nuget/Utility";
import * as pkgLocationUtils from "packaging-common/locationUtilities";
import { logError } from 'packaging-common/util';

class PublishOptions implements INuGetCommandOptions {
constructor(
Expand All @@ -28,7 +29,7 @@ async function main(): Promise<void> {
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.NuGet);
} catch (error) {
tl.debug("Unable to get packaging URIs, using default collection URI");
tl.debug(JSON.stringify(error));
logError(error);
const collectionUrl = tl.getVariable("System.TeamFoundationCollectionUri");
packagingLocation = {
PackagingUris: [collectionUrl],
Expand Down
3 changes: 2 additions & 1 deletion Tasks/NuGetRestoreV1/nugetinstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import peParser = require('packaging-common/pe-parser/index');
import {VersionInfo} from "packaging-common/pe-parser/VersionResource";
import * as pkgLocationUtils from "packaging-common/locationUtilities";
import { getProjectAndFeedIdFromInputParam } from "packaging-common/util"
import { logError } from 'packaging-common/util';

const NUGET_ORG_V2_URL: string = "https://www.nuget.org/api/v2/";
const NUGET_ORG_V3_URL: string = "https://api.nuget.org/v3/index.json";
Expand All @@ -35,7 +36,7 @@ async function main(): Promise<void> {
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.NuGet);
} catch (error) {
tl.debug("Unable to get packaging URIs, using default collection URI");
tl.debug(JSON.stringify(error));
logError(error);
const collectionUrl = tl.getVariable("System.TeamFoundationCollectionUri");
packagingLocation = {
PackagingUris: [collectionUrl],
Expand Down
3 changes: 2 additions & 1 deletion Tasks/NuGetV0/nuget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as auth from "packaging-common/nuget/Authentication";
import nuGetGetter = require("packaging-common/nuget/NuGetToolGetter");
import peParser = require('packaging-common/pe-parser/index');
import * as pkgLocationUtils from "packaging-common/locationUtilities";
import { logError } from 'packaging-common/util';

class NuGetExecutionOptions {
constructor(
Expand All @@ -23,7 +24,7 @@ async function main(): Promise<void> {
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.NuGet);
} catch (error) {
tl.debug("Unable to get packaging URIs, using default collection URI");
tl.debug(JSON.stringify(error));
logError(error);
const collectionUrl = tl.getVariable("System.TeamFoundationCollectionUri");
packagingLocation = {
PackagingUris: [collectionUrl],
Expand Down
Loading

0 comments on commit b59a667

Please sign in to comment.