Skip to content

Commit b17c56a

Browse files
authored
Merge pull request #112 from FlowCI/feature/api/yml_error_info
Feature/api/yml error info
2 parents 2ff5668 + 0cc8a78 commit b17c56a

File tree

5 files changed

+38
-92
lines changed

5 files changed

+38
-92
lines changed

platform-api/src/main/java/com/flow/platform/api/util/CommonUtil.java

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

1717
package com.flow.platform.api.util;
1818

19-
import com.google.common.base.Strings;
2019
import java.math.BigInteger;
2120
import java.time.LocalDateTime;
2221
import java.time.format.DateTimeFormatter;

platform-api/src/main/java/com/flow/platform/api/util/NodeUtil.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
import com.flow.platform.api.domain.node.Node;
2121
import com.flow.platform.api.exception.YmlException;
2222
import com.flow.platform.yml.parser.YmlParser;
23-
import com.flow.platform.yml.parser.exception.YmlParseException;
2423
import com.flow.platform.yml.parser.exception.YmlFormatException;
25-
import com.google.common.base.Strings;
24+
import com.flow.platform.yml.parser.exception.YmlParseException;
2625
import com.google.common.io.Files;
2726
import java.io.File;
2827
import java.nio.file.Path;
@@ -72,10 +71,8 @@ public static Node buildFromYml(String yml, String root) {
7271
Flow[] flows;
7372
try {
7473
flows = YmlParser.fromYml(yml, Flow[].class);
75-
} catch (YmlParseException e) {
76-
throw new YmlException("Yml parser error", e);
77-
} catch (YmlFormatException e) {
78-
throw new YmlException("Yml validate error", e);
74+
} catch (YmlParseException | YmlFormatException e) {
75+
throw new YmlException(e.getMessage());
7976
}
8077

8178
// current version only support single flow

platform-yml-parser/src/main/java/com/flow/platform/yml/parser/YmlParser.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ public class YmlParser {
3434

3535
private final static YamlConfig yamlConfig = new YamlConfig();
3636

37+
private final static String YML_ILLEGAL_MESSAGE = "yml format is illegal";
38+
private final static String YML_CONVERT_MESSAGE = "Object to yml error, please retry";
39+
3740
static {
3841
yamlConfig.writeConfig.setAutoAnchor(false);
3942
yamlConfig.writeConfig.setWriteClassname(WriteClassName.NEVER);
4043
}
4144

4245
private static <T> T fromObject(Object o, Type typeOfT) {
4346
if (o == null) {
44-
throw new YmlParseException("yml parser error");
47+
throw new YmlParseException(YML_ILLEGAL_MESSAGE);
4548
}
4649
return (T) TypeAdaptorFactory.getAdaptor(typeOfT).read(o);
4750
}
@@ -63,7 +66,7 @@ public static <T> T fromYml(String str, Type typeOfT) {
6366
YamlReader yamlReader = new YamlReader(str, yamlConfig);
6467
result = (Map) yamlReader.read();
6568
} catch (Throwable throwable) {
66-
throw new YmlParseException("Load Yml error");
69+
throw new YmlParseException(YML_ILLEGAL_MESSAGE);
6770
}
6871
return fromObject(result.get("flow"), typeOfT);
6972
}
@@ -78,14 +81,14 @@ public static <T> String toYml(T t) {
7881
Map<String, Object> map = new HashMap<>();
7982
Object o = toObject(t);
8083
map.put("flow", o);
81-
String yml = null;
84+
String yml;
8285

8386
try (Writer stringWriter = new StringWriter()) {
8487
YamlWriter writer = new YamlWriter(stringWriter, yamlConfig);
8588
writer.write(map);
8689
yml = stringWriter.toString();
8790
} catch (Throwable throwable) {
88-
throw new YmlParseException(String.format("Object to yaml error"), throwable);
91+
throw new YmlParseException(YML_CONVERT_MESSAGE, throwable);
8992
}
9093

9194
return yml;

platform-yml-parser/src/main/java/com/flow/platform/yml/parser/adaptor/YmlAdaptor.java

Lines changed: 26 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.lang.reflect.Field;
2828
import java.lang.reflect.Method;
2929
import java.lang.reflect.Type;
30-
import java.util.HashMap;
3130
import java.util.LinkedHashMap;
3231
import java.util.Map;
3332

@@ -65,7 +64,7 @@ protected <T> T doRead(Object o, Class<T> clazz) {
6564
try {
6665
instance = clazz.newInstance();
6766
} catch (Throwable throwable) {
68-
throw new YmlParseException(String.format("clazz - %s instance error", clazz.getName()), throwable);
67+
throw new YmlParseException(String.format("clazz '%s' create instance error ", clazz.getName()), throwable);
6968
}
7069

7170
Class<?> raw = clazz;
@@ -80,12 +79,12 @@ protected <T> T doRead(Object o, Class<T> clazz) {
8079
YmlSerializer ymlSerializer = field.getAnnotation(YmlSerializer.class);
8180

8281
//filter no annotations field
83-
if (noAnnotationField(field)) {
82+
if (isNullOrEmpty(ymlSerializer)) {
8483
continue;
8584
}
8685

8786
//filter ignore field
88-
if (ignoreField(field)) {
87+
if (isIgnore(ymlSerializer)) {
8988
continue;
9089
}
9190
setValueToField(o, field, instance, ymlSerializer, clazz);
@@ -96,12 +95,12 @@ protected <T> T doRead(Object o, Class<T> clazz) {
9695
YmlSerializer ymlSerializer = method.getAnnotation(YmlSerializer.class);
9796

9897
//filter no annotations method
99-
if (noAnnotationMethod(method)) {
98+
if (isNullOrEmpty(ymlSerializer)) {
10099
continue;
101100
}
102101

103102
//filter ignore method
104-
if (ignoreMethod(method)) {
103+
if (isIgnore(ymlSerializer)) {
105104
continue;
106105
}
107106

@@ -120,9 +119,9 @@ private <T> void setValueToField(Object o, Field field, T instance, YmlSerialize
120119
Object obj = ((Map) o).get(getAnnotationMappingName(field.getName(), ymlSerializer));
121120

122121
// required field
123-
if (requiredField(field)) {
122+
if (requiredField(ymlSerializer)) {
124123
if (obj == null) {
125-
throw new YmlParseException(String.format("required field - %s", field.getName()));
124+
throw new YmlParseException(String.format("field '%s' is missing", field.getName()));
126125
}
127126
}
128127

@@ -163,18 +162,18 @@ protected <T> Object doWrite(Field field, T t, YmlSerializer ymlSerializer) {
163162
try {
164163
return TypeAdaptorFactory.getAdaptor(fieldType).write(field.get(t));
165164
} catch (Throwable throwable) {
166-
throw new YmlParseException(String.format("field - %s get error", field.getName()), throwable);
165+
throw new YmlParseException(String.format("field '%s' is get error", field.getName()), throwable);
167166
}
168167
}
169168

170169
//annotation provider adaptor
171170
if (ymlSerializer.adaptor() != EmptyAdapter.class) {
172171
try {
173-
YmlAdaptor instance = (YmlAdaptor) ymlSerializer.adaptor().newInstance();
172+
YmlAdaptor instance = ymlSerializer.adaptor().newInstance();
174173
return instance.write(field.get(t));
175174
} catch (Throwable throwable) {
176175
throw new YmlParseException(
177-
String.format("create instance adaptor - %s error", ymlSerializer.adaptor().getName()), throwable);
176+
String.format("create instance '%s' adaptor error", ymlSerializer.adaptor().getName()), throwable);
178177
}
179178
}
180179

@@ -193,11 +192,12 @@ public static <T> void validate(Field field, T instance, YmlSerializer ymlSerial
193192
value = field.get(instance);
194193
} catch (Throwable throwable) {
195194
throw new YmlFormatException(String
196-
.format("validate - %s instance error - %s", ymlSerializer.validator().getName(), throwable));
195+
.format("field '%s' is validate error %s", ymlSerializer.validator().getName(),
196+
throwable.getMessage()));
197197
}
198198

199199
if (validator.validate(value) == false) {
200-
throw new YmlFormatException(String.format("field - %s , validate - error", field.getName()));
200+
throw new YmlFormatException(String.format("field '%s' is validate error", field.getName()));
201201
}
202202

203203
}
@@ -254,7 +254,7 @@ public <T> Object doWrite(T clazz) {
254254
// 获取 field 对应的值
255255
YmlSerializer ymlSerializer = field.getAnnotation(YmlSerializer.class);
256256

257-
if (noAnnotationField(field)) {
257+
if (isNullOrEmpty(ymlSerializer)) {
258258
continue;
259259
}
260260

@@ -267,7 +267,7 @@ public <T> Object doWrite(T clazz) {
267267
// 获取 field 对应的值
268268
YmlSerializer ymlSerializer = method.getAnnotation(YmlSerializer.class);
269269

270-
if (noAnnotationMethod(method)) {
270+
if (isNullOrEmpty(ymlSerializer)) {
271271
continue;
272272
}
273273

@@ -300,12 +300,10 @@ private String getAnnotationMappingName(String s, YmlSerializer ymlSerializer) {
300300

301301
/**
302302
* detect required field from annotation
303-
*
304-
* @param field field
305-
* @return True or false
303+
* @param annotation
304+
* @return
306305
*/
307-
private Boolean requiredField(Field field) {
308-
YmlSerializer annotation = field.getAnnotation(YmlSerializer.class);
306+
private Boolean requiredField(YmlSerializer annotation) {
309307
if (annotation == null) {
310308
return false;
311309
}
@@ -318,10 +316,11 @@ private Boolean requiredField(Field field) {
318316
}
319317

320318
/**
321-
* filter no annotation field
319+
* detect annotation has or not
320+
* @param annotation
321+
* @return
322322
*/
323-
private Boolean noAnnotationField(Field field) {
324-
YmlSerializer annotation = field.getAnnotation(YmlSerializer.class);
323+
private Boolean isNullOrEmpty(YmlSerializer annotation) {
325324
if (annotation == null) {
326325
return true;
327326
}
@@ -330,10 +329,11 @@ private Boolean noAnnotationField(Field field) {
330329
}
331330

332331
/**
333-
* filter ignore field
332+
* filter ignore action
333+
* @param annotation
334+
* @return
334335
*/
335-
private Boolean ignoreField(Field field) {
336-
YmlSerializer annotation = field.getAnnotation(YmlSerializer.class);
336+
private Boolean isIgnore(YmlSerializer annotation) {
337337
if (annotation.ignore()) {
338338
return true;
339339
}
@@ -342,55 +342,6 @@ private Boolean ignoreField(Field field) {
342342
}
343343

344344

345-
/**
346-
* filter no annotation method
347-
*/
348-
private Boolean noAnnotationMethod(Method method) {
349-
YmlSerializer annotation = method.getAnnotation(YmlSerializer.class);
350-
if (annotation == null) {
351-
return true;
352-
}
353-
354-
return false;
355-
}
356-
357-
/**
358-
* filter ignore method
359-
*/
360-
private Boolean ignoreMethod(Method method) {
361-
YmlSerializer annotation = method.getAnnotation(YmlSerializer.class);
362-
if (annotation.ignore()) {
363-
return true;
364-
}
365-
366-
return false;
367-
}
368-
369-
370-
/**
371-
* get all fields
372-
*
373-
* @return Map<name, field>
374-
*/
375-
private Map<String, Field> allFields(Class<?> clazz) {
376-
Map<String, Field> map = new HashMap<>();
377-
378-
while (true) {
379-
if (clazz == null) {
380-
break;
381-
}
382-
383-
for (Field field : clazz.getDeclaredFields()) {
384-
map.put(field.getName(), field);
385-
}
386-
387-
clazz = clazz.getSuperclass();
388-
}
389-
390-
return map;
391-
}
392-
393-
394345
private String fieldNameForSetterGetter(String fieldName) {
395346
return Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
396347
}
@@ -411,10 +362,6 @@ private Method getGetterMethod(Field field, Class<?> clazz) {
411362
return getMethod(field, clazz, "get");
412363
}
413364

414-
private Method getSetterMethod(Field field, Class<?> clazz) {
415-
return getMethod(field, clazz, "set");
416-
}
417-
418365
/**
419366
* get method
420367
*/

platform-yml-parser/src/main/java/com/flow/platform/yml/parser/exception/YmlException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
public class YmlException extends RuntimeException {
2323

2424
public YmlException(String message) {
25-
super(message);
25+
super("Illegal yml format: " + message);
2626
}
2727

2828
public YmlException(String message, Throwable cause) {
29-
super(message, cause);
29+
super("Illegal yml format: " + message, cause);
3030
}
3131
}

0 commit comments

Comments
 (0)