Skip to content

Commit 12a163e

Browse files
authored
Support for additional fields in the error message format (#63)
* Additional message fields
1 parent 20e1177 commit 12a163e

File tree

18 files changed

+260
-67
lines changed

18 files changed

+260
-67
lines changed

.circleci/config.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ jobs:
1010
- checkout
1111
- restore_cache: &keys
1212
keys:
13-
- v1-zowe-sdk-{{ .Branch }}
14-
- v1-zowe-sdk-master
13+
- v2-zowe-sdk-{{ .Branch }}
14+
- v2-zowe-sdk-master
1515
- run: &build_jarpatcher
1616
name: Building JAR Patcher using Gradle
1717
command: ./gradlew :jarpatcher:build
@@ -24,6 +24,7 @@ jobs:
2424
- run: &zosbuild
2525
name: Building Native z/OS Code
2626
command: |
27+
npm uninstall zowe-api-dev
2728
npm install
2829
source .circleci/river.env
2930
npm run zosbuild
@@ -64,7 +65,7 @@ jobs:
6465
- ~/code/zowe-api-dev/node_modules
6566
- ~/code/zowe-rest-api-sample-spring/.gradle
6667
- ~/code/gradle/wrapper/gradle-wrapper.jar
67-
key: v1-zowe-sdk-{{ .Branch }}-{{ epoch }}
68+
key: v2-zowe-sdk-{{ .Branch }}-{{ epoch }}
6869
publish_all:
6970
docker:
7071
- image: circleci/openjdk:8-jdk-node

scripts/run-integration-tests-zos.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
scripts/lock-port.sh
33

44
cd zowe-rest-api-sample-spring
5-
zowe-api-dev deploy
5+
zowe-api-dev deploy --force
66
zowe-api-dev config --name zos -p port=$TEST_PORT
77
zowe-api-dev start --job
88

zowe-api-dev/src/commands/deploy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default class Deploy extends Command {
88
static description = "deploy the API service artifacts to z/OS";
99

1010
static flags = {
11-
force: flags.boolean({ char: "f", description: "forces full deployment even if there is not change" }),
11+
force: flags.boolean({ char: "f", description: "forces full deployment even if there is not change", default: true }),
1212
};
1313

1414
async run() {

zowe-api-dev/src/files.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export function transferFiles(
5858
console.time("transferFile");
5959
checkZoweProfileName(userConfig);
6060
for (const [file, options] of Object.entries(files)) {
61+
debug("options", options);
6162
const zosFile = `${zosTargetDir}/${options.target}`;
6263
const zosDir = dirname(zosFile);
6364
let soUpdated = true;
@@ -106,12 +107,13 @@ export function transferFiles(
106107
}
107108
}
108109
const postCommands: string[] = [];
109-
if (soUpdated && options.postSoUpdateCommands) {
110+
if ((soUpdated || force) && options.postSoUpdateCommands) {
110111
postCommands.push(...options.postSoUpdateCommands);
111112
}
112113
if (options.postCommands) {
113114
postCommands.push(...options.postCommands);
114115
}
116+
debug("postCommands", postCommands);
115117
for (const postCommand of postCommands) {
116118
let finalCommand = postCommand;
117119
if (postCommand.startsWith("java") && userConfig.javaHome) {

zowe-rest-api-commons-spring/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ task zosbuild(type: Exec) {
8282
if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows')) {
8383
commandLine 'zowe-api-dev.cmd', 'zosbuild'
8484
} else {
85-
commandLine 'zowe-api-dev', 'zosbuild'
85+
commandLine 'npx', 'zowe-api-dev', 'zosbuild'
8686
}
8787
}
8888

zowe-rest-api-commons-spring/src/main/java/org/zowe/commons/error/ErrorMessage.java

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,23 @@
1111

1212
import org.zowe.commons.rest.response.MessageType;
1313

14+
import lombok.AllArgsConstructor;
15+
import lombok.Data;
16+
import lombok.NoArgsConstructor;
17+
18+
@Data
19+
@AllArgsConstructor
20+
@NoArgsConstructor
1421
public class ErrorMessage {
1522
private String key;
1623
private String number;
1724
private MessageType type;
1825
private String text;
19-
20-
public ErrorMessage() {
21-
}
26+
private String reason;
27+
private String action;
28+
private String component;
2229

2330
public ErrorMessage(String key, String number, MessageType type, String text) {
24-
this.key = key;
25-
this.number = number;
26-
this.type = type;
27-
this.text = text;
28-
}
29-
30-
public String getKey() {
31-
return key;
32-
}
33-
34-
public void setKey(String key) {
35-
this.key = key;
36-
}
37-
38-
public String getNumber() {
39-
return number;
40-
}
41-
42-
public void setNumber(String number) {
43-
this.number = number;
44-
}
45-
46-
public MessageType getType() {
47-
return type;
48-
}
49-
50-
public void setType(MessageType type) {
51-
this.type = type;
52-
}
53-
54-
public String getText() {
55-
return text;
56-
}
57-
58-
public void setText(String text) {
59-
this.text = text;
31+
this(key, number, type, text, null, null, null);
6032
}
6133
}

zowe-rest-api-commons-spring/src/main/java/org/zowe/commons/error/ErrorService.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
*/
1010
package org.zowe.commons.error;
1111

12-
import org.zowe.commons.rest.response.ApiMessage;
13-
1412
import java.util.List;
1513

14+
import org.zowe.commons.rest.response.ApiMessage;
15+
1616
/**
1717
* Service for creating {@link ApiMessage} by string key and list of parameters.
1818
* See default implementation {@link org.zowe.commons.error.ErrorServiceImpl}.
@@ -56,4 +56,19 @@ public interface ErrorService {
5656
* @return Readable text for the given message key.
5757
*/
5858
String getReadableMessage(String key, Object... parameters);
59+
60+
/**
61+
* @return Returns the value of the default message source attribute that is
62+
* added to every error message. It is a string that identifies the
63+
* source service. For example: myhost:8080:serviceid
64+
*/
65+
String getDefaultMessageSource();
66+
67+
/**
68+
* @param defaultMessageSource The value of the default message source attribute
69+
* that is added to every error message. It is a
70+
* string that identifies the source service. For
71+
* example: myhost:8080:serviceid
72+
*/
73+
void setDefaultMessageSource(String defaultMessageSource);
5974
}

zowe-rest-api-commons-spring/src/main/java/org/zowe/commons/error/ErrorServiceImpl.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ public class ErrorServiceImpl implements ErrorService {
3636
private static final String INVALID_KEY_MESSAGE = "org.zowe.commons.error.invalidMessageKey";
3737
private static final String INVALID_MESSAGE_TEXT_FORMAT = "org.zowe.commons.error.invalidMessageTextFormat";
3838
private static final Logger LOGGER = LoggerFactory.getLogger(ErrorServiceImpl.class);
39+
private static final int STACK_TRACE_ELEMENT_ABOVE_CREATEAPIMESSAGE_METHOD = 3;
3940

4041
private final ErrorMessageStorage messageStorage;
42+
private String defaultMessageSource;
4143

4244
/**
4345
* Constructor that creates only common messages.
@@ -103,6 +105,16 @@ public void loadMessages(String messagesFilePath) {
103105
}
104106
}
105107

108+
@Override
109+
public String getDefaultMessageSource() {
110+
return defaultMessageSource;
111+
}
112+
113+
@Override
114+
public void setDefaultMessageSource(String defaultMessageSource) {
115+
this.defaultMessageSource = defaultMessageSource;
116+
}
117+
106118
/**
107119
* Internal method that call {@link ErrorMessageStorage} to get message by key.
108120
*
@@ -125,8 +137,13 @@ private Message createMessage(String key, Object... parameters) {
125137
messageParameters = validateParameters(message, key, parameters);
126138
text = String.format(message.getText(), messageParameters);
127139
}
128-
129-
return new BasicMessage(key, message.getType(), message.getNumber(), text);
140+
if (message.getComponent() == null) {
141+
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
142+
String className = stackTrace[STACK_TRACE_ELEMENT_ABOVE_CREATEAPIMESSAGE_METHOD].getClassName();
143+
message.setComponent(className);
144+
}
145+
return new BasicMessage(message.getType(), message.getNumber(), text, message.getReason(), message.getAction(),
146+
key, null, BasicMessage.generateMessageInstanceId(), defaultMessageSource, message.getComponent());
130147
}
131148

132149
/**
@@ -146,7 +163,7 @@ private ErrorMessage validateMessage(ErrorMessage message, String key) {
146163

147164
if (message == null) {
148165
String text = "Internal error: Invalid message key '%s' provided. No default message found. Please contact support of further assistance.";
149-
message = new ErrorMessage(INVALID_KEY_MESSAGE, "ZWEAS001", MessageType.ERROR, text);
166+
message = new ErrorMessage(INVALID_KEY_MESSAGE, "ZWEAS001", MessageType.ERROR, text, null, null, null);
150167
}
151168

152169
return message;

zowe-rest-api-commons-spring/src/main/java/org/zowe/commons/rest/response/BasicMessage.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
*/
1010
package org.zowe.commons.rest.response;
1111

12-
import org.zowe.commons.rest.response.Message;
13-
import org.zowe.commons.rest.response.MessageType;
14-
import com.fasterxml.jackson.annotation.JsonCreator;
15-
import com.fasterxml.jackson.annotation.JsonProperty;
16-
1712
import java.util.List;
1813
import java.util.Objects;
1914
import java.util.UUID;
2015

16+
import com.fasterxml.jackson.annotation.JsonCreator;
17+
import com.fasterxml.jackson.annotation.JsonProperty;
18+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
19+
2120
/**
2221
* Holds information about a {@link Message}. This class is immutable.
2322
* <p>
@@ -32,6 +31,7 @@
3231
*
3332
* @author Greg Berres, Petr Plavjanik
3433
*/
34+
@JsonPropertyOrder({ "messageType", "messageNumber", "messageContent", "messageReason", "messageAction", "messageKey", "messageParameters", "messageInstanceId", "messageComponent", "messageSource"})
3535
public class BasicMessage implements Message {
3636
private final MessageType messageType;
3737
private final String messageNumber;
@@ -80,7 +80,7 @@ public BasicMessage(@JsonProperty("messageKey") String messageKey,
8080
this(messageType, messageNumber, messageContent, null, null, messageKey, null, generateMessageInstanceId(), null, null);
8181
}
8282

83-
private static String generateMessageInstanceId() {
83+
public static String generateMessageInstanceId() {
8484
return UUID.randomUUID().toString();
8585
}
8686

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* This program and the accompanying materials are made available under the terms of the
3+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
4+
* https://www.eclipse.org/legal/epl-v20.html
5+
*
6+
* SPDX-License-Identifier: EPL-2.0
7+
*
8+
* Copyright Contributors to the Zowe Project.
9+
*/
10+
package org.zowe.commons.spring;
11+
12+
import java.net.InetAddress;
13+
import java.net.UnknownHostException;
14+
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.boot.context.event.ApplicationReadyEvent;
17+
import org.springframework.context.ApplicationListener;
18+
import org.springframework.context.annotation.Configuration;
19+
import org.springframework.core.env.Environment;
20+
import org.zowe.commons.error.CommonsErrorService;
21+
import org.zowe.commons.error.ErrorService;
22+
23+
import lombok.extern.slf4j.Slf4j;
24+
25+
@Configuration
26+
@Slf4j
27+
public class DefaultMessageSource implements ApplicationListener<ApplicationReadyEvent> {
28+
private final Environment environment;
29+
private final ErrorService errorService;
30+
31+
@Autowired
32+
public DefaultMessageSource(ErrorService errorService, Environment environment) {
33+
this.errorService = errorService;
34+
this.environment = environment;
35+
}
36+
37+
@Override
38+
public void onApplicationEvent(ApplicationReadyEvent event) {
39+
String hostname;
40+
try {
41+
hostname = InetAddress.getLocalHost().getCanonicalHostName();
42+
} catch (UnknownHostException e) {
43+
log.warn("Could not obtain hostname", e);
44+
hostname = "?";
45+
}
46+
String serviceId = environment.getProperty("apiml.service.serviceId");
47+
String messageSource = String.format("%s:%s:%s", hostname, environment.getProperty("server.port"), serviceId);
48+
errorService.setDefaultMessageSource(messageSource);
49+
CommonsErrorService.get().setDefaultMessageSource(messageSource);
50+
}
51+
}

0 commit comments

Comments
 (0)