Skip to content

Commit bb1780d

Browse files
authored
Put all android command code in try catch (#141)
* put in try catch, change token error to user friendly error * did same with list command
1 parent a0a7a89 commit bb1780d

File tree

2 files changed

+51
-49
lines changed

2 files changed

+51
-49
lines changed

src/commands/android.ts

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -136,57 +136,57 @@ androidCommand
136136
.option( '--dry-run', 'Preview the file that will be uploaded')
137137
.option('--debug', 'Enable debug logs')
138138
.action(async (options: UploadAndroidOptions) => {
139-
const token = validateAndPrepareToken(options);
140-
141-
if (!options.realm || options.realm.trim() === '') {
142-
androidCommand.error(COMMON_ERROR_MESSAGES.REALM_NOT_SPECIFIED);
143-
}
144-
145139
const spinner = createSpinner();
146140
const logger = createLogger(options.debug ? LogLevel.DEBUG : LogLevel.INFO, spinner);
147141

148-
logger.debug(`Validating App ID: ${options.appId}`);
149-
if (!isValidAppId(options.appId)) {
150-
throw new UserFriendlyError(null, 'Invalid Application ID. It must be a non-empty string.');
151-
}
142+
try {
143+
const token = validateAndPrepareToken(options);
152144

153-
logger.debug(`Validating Version Code: ${options.versionCode}`);
154-
if (!isValidVersionCode(options.versionCode)) {
155-
throw new UserFriendlyError(null, 'Invalid Version Code. It must be an integer.');
156-
}
145+
if (!options.realm || options.realm.trim() === '') {
146+
androidCommand.error(COMMON_ERROR_MESSAGES.REALM_NOT_SPECIFIED);
147+
}
157148

158-
logger.debug(`Validating Mapping File Path: ${options.path}`);
159-
if (!isValidFile(options.path)) {
160-
throw new UserFriendlyError(null, `Invalid mapping file path: ${options.path}.`);
161-
}
149+
logger.debug(`Validating App ID: ${options.appId}`);
150+
if (!isValidAppId(options.appId)) {
151+
throw new UserFriendlyError(null, 'Invalid Application ID. It must be a non-empty string.');
152+
}
162153

163-
logger.debug(`Validating Mapping File Extension`);
164-
if (!hasValidExtension(options.path, '.txt', '.gz')) {
165-
throw new UserFriendlyError(null, `Mapping file does not have correct extension: ${options.path}.`);
166-
}
154+
logger.debug(`Validating Version Code: ${options.versionCode}`);
155+
if (!isValidVersionCode(options.versionCode)) {
156+
throw new UserFriendlyError(null, 'Invalid Version Code. It must be an integer.');
157+
}
167158

168-
logger.debug(`Validating optional Splunk Build ID: ${options.splunkBuildId}`);
169-
if (options.splunkBuildId && !isValidSplunkBuildId(options.splunkBuildId)) {
170-
throw new UserFriendlyError(null, 'Error: Invalid Splunk Build ID. It must be a non-empty string.');
171-
}
159+
logger.debug(`Validating Mapping File Path: ${options.path}`);
160+
if (!isValidFile(options.path)) {
161+
throw new UserFriendlyError(null, `Invalid mapping file path: ${options.path}.`);
162+
}
163+
164+
logger.debug(`Validating Mapping File Extension`);
165+
if (!hasValidExtension(options.path, '.txt', '.gz')) {
166+
throw new UserFriendlyError(null, `Mapping file does not have correct extension: ${options.path}.`);
167+
}
168+
169+
logger.debug(`Validating optional Splunk Build ID: ${options.splunkBuildId}`);
170+
if (options.splunkBuildId && !isValidSplunkBuildId(options.splunkBuildId)) {
171+
throw new UserFriendlyError(null, 'Error: Invalid Splunk Build ID. It must be a non-empty string.');
172+
}
172173

173-
logger.info(`Preparing to upload Android mapping file:
174+
logger.info(`Preparing to upload Android mapping file:
174175
File: ${options.path}
175176
App ID: ${options.appId}
176177
Version Code: ${options.versionCode}
177178
Splunk Build ID: ${options.splunkBuildId || 'Not provided'}`);
178179

179-
if (options.dryRun) {
180-
logger.info('Dry Run complete - No file will be uploaded.');
181-
return;
182-
}
180+
if (options.dryRun) {
181+
logger.info('Dry Run complete - No file will be uploaded.');
182+
return;
183+
}
183184

184-
const url = generateURL('upload', options.realm, options.appId, options.versionCode, options.splunkBuildId);
185-
logger.debug(`URL Endpoint: ${url}`);
185+
const url = generateURL('upload', options.realm, options.appId, options.versionCode, options.splunkBuildId);
186+
logger.debug(`URL Endpoint: ${url}`);
186187

187-
spinner.start(`Uploading Android mapping file: ${options.path}`);
188+
spinner.start(`Uploading Android mapping file: ${options.path}`);
188189

189-
try {
190190
const axiosInstance = axios.create();
191191
attachApiInterceptor(axiosInstance, logger, url, {
192192
userFriendlyMessage: 'An error occurred during mapping file upload.'
@@ -242,16 +242,16 @@ androidCommand
242242
.option('--dry-run', 'Preview the file that will be uploaded and the parameters extracted from the AndroidManifest.xml file')
243243
.option('--debug', 'Enable debug logs')
244244
.action(async (options: UploadAndroidWithManifestOptions) => {
245-
const token = validateAndPrepareToken(options);
246-
247-
if (!options.realm || options.realm.trim() === '') {
248-
androidCommand.error(COMMON_ERROR_MESSAGES.REALM_NOT_SPECIFIED);
249-
}
250-
251245
const spinner = createSpinner();
252246
const logger = createLogger(options.debug ? LogLevel.DEBUG : LogLevel.INFO, spinner);
253247

254248
try {
249+
const token = validateAndPrepareToken(options);
250+
251+
if (!options.realm || options.realm.trim() === '') {
252+
androidCommand.error(COMMON_ERROR_MESSAGES.REALM_NOT_SPECIFIED);
253+
}
254+
255255
logger.debug(`Validating Mapping File Path: ${options.path}`);
256256
if (!isValidFile(options.path)) {
257257
throw new UserFriendlyError(null, `Invalid mapping file path: ${options.path}.`);
@@ -359,16 +359,17 @@ androidCommand
359359
.option('--debug',
360360
'Enable debug logs')
361361
.action(async (options) => {
362-
const token = validateAndPrepareToken(options);
363-
364-
if (!options.realm || options.realm.trim() === '') {
365-
androidCommand.error(COMMON_ERROR_MESSAGES.REALM_NOT_SPECIFIED);
366-
}
367-
368362
const logger = createLogger(options.debug ? LogLevel.DEBUG : LogLevel.INFO);
369-
const url = generateURL('list', options.realm, options.appId);
370363

371364
try {
365+
const token = validateAndPrepareToken(options);
366+
367+
if (!options.realm || options.realm.trim() === '') {
368+
androidCommand.error(COMMON_ERROR_MESSAGES.REALM_NOT_SPECIFIED);
369+
}
370+
371+
const url = generateURL('list', options.realm, options.appId);
372+
372373
logger.debug(`URL Endpoint: ${url}`);
373374

374375
const axiosInstance = axios.create();

src/utils/inputValidations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import fs from 'fs';
1818
import path from 'path';
19+
import { UserFriendlyError } from './userFriendlyErrors';
1920

2021
export const COMMON_ERROR_MESSAGES = {
2122
TOKEN_NOT_SPECIFIED: 'Error: API access token is required. Please pass it into the command as the --token option, or set using the environment variable SPLUNK_ACCESS_TOKEN',
@@ -53,7 +54,7 @@ export const isValidSplunkBuildId = (splunkBuildId: unknown | undefined): boolea
5354
export function validateAndPrepareToken(options: { token?: string }): string {
5455
const token = options.token || process.env.SPLUNK_ACCESS_TOKEN;
5556
if (!token) {
56-
throw new Error(COMMON_ERROR_MESSAGES.TOKEN_NOT_SPECIFIED);
57+
throw new UserFriendlyError(null, COMMON_ERROR_MESSAGES.TOKEN_NOT_SPECIFIED);
5758
}
5859
return token;
5960
}

0 commit comments

Comments
 (0)