-
Notifications
You must be signed in to change notification settings - Fork 85
/
ZosmfRestClient.ts
103 lines (93 loc) · 4.26 KB
/
ZosmfRestClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import { IImperativeError, Logger, RestClient, TextUtils, RestConstants, SessConstants } from "@zowe/imperative";
import { ZosmfHeaders } from "./ZosmfHeaders";
/**
* Wrapper for invoke z/OSMF API through the RestClient to perform common error
* handling and checking and resolve promises according to generic types
* @export
* @class ZosmfRestClient
* @extends {RestClient}
*/
export class ZosmfRestClient extends RestClient {
/**
* Use the Brightside logger instead of the imperative logger
* @type {Logger}
*/
public get log(): Logger {
return Logger.getAppLogger();
}
/**
* Append z/OSMF specific headers to the callers headers for cases
* where a header is common to every request.
* @param {any[] | undefined} headers - current header array
* @memberof ZosmfRestClient
*/
protected appendHeaders(headers: any[] | undefined): any[] {
if (headers == null) {
headers = [ZosmfHeaders.X_CSRF_ZOSMF_HEADER];
} else {
headers.push(ZosmfHeaders.X_CSRF_ZOSMF_HEADER);
}
return headers;
}
/**
* Process an error encountered in the rest client
* @param {IImperativeError} original - the original error automatically built by the abstract rest client
* @returns {IImperativeError} - the processed error with details added
* @memberof ZosmfRestClient
*/
protected processError(original: IImperativeError): IImperativeError {
original.msg = "z/OSMF REST API Error:\n" + original.msg;
let details = original.causeErrors;
try {
const json = JSON.parse(details);
// if we didn't get an error trying to parse json, check if there is a stack
// on the JSON error and delete it
if (json.stack != null) {
this.log.error("An error was encountered in z/OSMF with a stack." +
" Here is the full error before deleting the stack:\n%s", JSON.stringify(json));
this.log.error("The stack has been deleted from the error before displaying the error to the user");
delete json.stack; // remove the stack field
}
// if we didn't get an error, make the parsed details part of the error
details = TextUtils.prettyJson(json, undefined, false);
} catch (e) {
// if there's an error, the causeErrors text is not json
this.log.debug("Encountered an error trying to parse causeErrors as JSON - causeErrors is likely not JSON format");
}
original.msg += "\n" + details; // add the data string which is the original error
if (this.response && this.response.statusCode === RestConstants.HTTP_STATUS_401) {
original.msg = "This operation requires authentication.\n\n" + original.msg +
"\nHost: " + this.session.ISession.hostname +
"\nPort: " + this.session.ISession.port +
"\nBase Path: " + this.session.ISession.basePath +
"\nResource: " + this.mResource +
"\nRequest: " + this.mRequest +
"\nHeaders: " + JSON.stringify(this.mReqHeaders) +
"\nPayload: " + this.mRequest +
"\n"
;
if (this.session.ISession.type === SessConstants.AUTH_TYPE_BASIC) {
original.additionalDetails = "Username or password are not valid or expired.\n\n";
}
if (this.session.ISession.type === SessConstants.AUTH_TYPE_TOKEN) {
original.additionalDetails = "Token is not valid or expired.\n\n" +
"For CLI usage, see `zowe auth login apiml --help`";
}
// TODO: Add PFX support in the future
if (this.session.ISession.type === SessConstants.AUTH_TYPE_CERT_PEM) {
original.additionalDetails = "Certificate is not valid or expired.\n\n";
}
}
return original;
}
}