Skip to content

Commit 2f07f41

Browse files
authored
Merge pull request #80 from pcoop/branch1
Error handling for Create failures
2 parents 96b96ed + 9c9e3ef commit 2f07f41

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

__tests__/api/BundlePush/BundlePusher.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111

1212
import { BundlePusher } from "../../../src/api/BundlePush/BundlePusher";
13-
import { IHandlerParameters } from "@zowe/imperative";
13+
import { IHandlerParameters, ImperativeError, IImperativeError } from "@zowe/imperative";
1414
import * as PushBundleDefinition from "../../../src/cli/push/bundle/PushBundle.definition";
1515
import * as fse from "fs-extra";
1616
import * as fs from "fs";
@@ -190,6 +190,45 @@ describe("BundlePusher01", () => {
190190
expect(sshSpy).toHaveBeenCalledTimes(1);
191191
expect(createSpy).toHaveBeenCalledTimes(1);
192192
});
193+
it("should complain if remote target dir can't be found", async () => {
194+
createSpy.mockImplementationOnce(() => {
195+
const cause = "{ \"category\": 8, \"rc\": -1, \"reason\": 93651005 }";
196+
const impError: IImperativeError = { msg: "Injected Create error", causeErrors: cause };
197+
throw new ImperativeError(impError);
198+
});
199+
await runPushTestWithError("__tests__/__resources__/ExampleBundle01", false,
200+
"The target directory does not exist, consider creating it by issuing: \nzowe zos-uss issue ssh \"mkdir -p /u/ThisDoesNotExist\"");
201+
202+
expect(zosMFSpy).toHaveBeenCalledTimes(1);
203+
expect(sshSpy).toHaveBeenCalledTimes(1);
204+
expect(createSpy).toHaveBeenCalledTimes(1);
205+
});
206+
it("should complain if remote bundle dir not auth", async () => {
207+
createSpy.mockImplementationOnce(() => {
208+
const cause = "{ \"category\": 8, \"rc\": -1, \"reason\": -276865003 }";
209+
const impError: IImperativeError = { msg: "Injected Create error", causeErrors: cause };
210+
throw new ImperativeError(impError);
211+
});
212+
await runPushTestWithError("__tests__/__resources__/ExampleBundle01", false,
213+
"You are not authorized to create the target bundle directory '/u/ThisDoesNotExist/12345678'.");
214+
215+
expect(zosMFSpy).toHaveBeenCalledTimes(1);
216+
expect(sshSpy).toHaveBeenCalledTimes(1);
217+
expect(createSpy).toHaveBeenCalledTimes(1);
218+
});
219+
it("should not complain if remote bundle dir already exists", async () => {
220+
createSpy.mockImplementationOnce(() => {
221+
const cause = "{ \"category\": 1, \"rc\": 4, \"reason\": 19 }";
222+
const impError: IImperativeError = { msg: "Injected Create error", causeErrors: cause };
223+
throw new ImperativeError(impError);
224+
});
225+
await runPushTest("__tests__/__resources__/ExampleBundle01", false,
226+
"PUSH operation completed.");
227+
228+
expect(zosMFSpy).toHaveBeenCalledTimes(1);
229+
expect(sshSpy).toHaveBeenCalledTimes(1);
230+
expect(createSpy).toHaveBeenCalledTimes(1);
231+
});
193232
it("should complain if remote bundle dir error", async () => {
194233
listSpy.mockImplementationOnce(() => { throw new Error( "Injected List error" ); });
195234
await runPushTestWithError("__tests__/__resources__/ExampleBundle01", false, "Injected List error");

src/api/BundlePush/BundlePusher.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,23 +263,38 @@ export class BundlePusher {
263263

264264
const WARNING = 4;
265265
const ALREADY_EXISTS = 19;
266+
const EIGHT = 8;
267+
const TARGET_DIR_NOT_EXIST = 93651005;
268+
const NO_PERMISSION = -276865003;
266269
try {
267270
await Create.uss(zosMFSession, this.params.arguments.bundledir, "directory");
268271
}
269272
catch (error) {
270-
try {
271-
const json = JSON.parse(error.causeErrors);
272-
if (json.category === 1 &&
273-
json.rc === WARNING &&
274-
json.reason === ALREADY_EXISTS) {
275-
// if it already exists, no worries
276-
return;
273+
if (error.causeErrors !== undefined)
274+
{
275+
const cause = JSON.parse(error.causeErrors);
276+
277+
// Special case some known errors
278+
if (cause !== undefined) {
279+
if (cause.category === 1 &&
280+
cause.rc === WARNING &&
281+
cause.reason === ALREADY_EXISTS) {
282+
// if it already exists, no worries
283+
return;
284+
}
285+
if (cause.category === EIGHT &&
286+
cause.rc === -1 &&
287+
cause.reason === TARGET_DIR_NOT_EXIST) {
288+
throw new Error("The target directory does not exist, consider creating it by issuing: \n" +
289+
"zowe zos-uss issue ssh \"mkdir -p " + this.params.arguments.targetdir + "\"");
290+
}
291+
if (cause.category === EIGHT &&
292+
cause.rc === -1 &&
293+
cause.reason === NO_PERMISSION) {
294+
throw new Error("You are not authorized to create the target bundle directory '" + this.params.arguments.bundledir + "'.");
295+
}
277296
}
278297
}
279-
catch (innerError) {
280-
// If this occurs we probably didn't receive an ImperativeError
281-
}
282-
283298
throw new Error("A problem occurred attempting to create directory '" + this.params.arguments.bundledir + "'. " +
284299
"Problem is: " + error.message);
285300
}

0 commit comments

Comments
 (0)