Преглед изворни кода

Explicit error handling methods for each catch clause

peter_backlund пре 18 година
родитељ
комит
937d223dc8

+ 35
- 9
dddsample/src/main/java/se/citerus/dddsample/application/ws/HandlingEventServiceEndpointImpl.java Прегледај датотеку

29
       Date date = parseIso8601Date(completionTime);
29
       Date date = parseIso8601Date(completionTime);
30
       TrackingId tid = new TrackingId(trackingId);
30
       TrackingId tid = new TrackingId(trackingId);
31
       CarrierMovementId cid;
31
       CarrierMovementId cid;
32
-      if (StringUtils.isNotBlank(carrierMovementId)) {
32
+      if (StringUtils.isNotEmpty(carrierMovementId)) {
33
         cid = new CarrierMovementId(carrierMovementId);
33
         cid = new CarrierMovementId(carrierMovementId);
34
       } else {
34
       } else {
35
         cid = null;
35
         cid = null;
39
       UnLocode ul = new UnLocode(unlocode);
39
       UnLocode ul = new UnLocode(unlocode);
40
 
40
 
41
       handlingEventService.register(date, tid, cid, ul, type);
41
       handlingEventService.register(date, tid, cid, ul, type);
42
+
43
+    } catch (IllegalArgumentException iae) {
44
+      handleIllegalArgument(iae);
45
+
42
     } catch (ParseException pe) {
46
     } catch (ParseException pe) {
43
-      logger.error("Invalid date format: " + completionTime + ", must be on ISO 8601 format: " + ISO_8601_FORMAT);
47
+      handleInvalidDateFormat(completionTime);
48
+
44
     } catch (UnknownTrackingIdException utid) {
49
     } catch (UnknownTrackingIdException utid) {
45
-      handleRetry(utid);
50
+      handleUnknownTrackingId(utid);
51
+
46
     } catch (UnknownCarrierMovementIdException ucmi) {
52
     } catch (UnknownCarrierMovementIdException ucmi) {
47
-      handleRetry(ucmi);
53
+      handleUnknownCarrierMovementId(ucmi);
54
+
48
     } catch (InvalidEventTypeException iete) {
55
     } catch (InvalidEventTypeException iete) {
49
-      logger.error(iete, iete);
56
+      handleInvalidEventType(iete);
57
+
50
     } catch (Exception e) {
58
     } catch (Exception e) {
51
-      logger.error(e, e);
59
+      handleOtherError(e);
52
     }
60
     }
53
-    // TODO: possibly handle "Duplicate event" exceptions due to unique constraint violations
61
+  }
62
+
63
+  private void handleIllegalArgument(IllegalArgumentException iae) {
64
+    logger.error(iae, iae);
65
+  }
66
+
67
+  private void handleOtherError(Exception e) {
68
+    logger.error(e, e);
69
+  }
70
+
71
+  private void handleInvalidEventType(InvalidEventTypeException iete) {
72
+    logger.error(iete, iete);
73
+  }
74
+
75
+  private void handleInvalidDateFormat(String completionTime) {
76
+    logger.error("Invalid date format: " + completionTime + ", must be on ISO 8601 format: " + ISO_8601_FORMAT);
54
   }
77
   }
55
 
78
 
56
   private HandlingEvent.Type parseEventType(final String eventType) throws InvalidEventTypeException {
79
   private HandlingEvent.Type parseEventType(final String eventType) throws InvalidEventTypeException {
61
     }
84
     }
62
   }
85
   }
63
 
86
 
64
-  private void handleRetry(Exception e) {
87
+  private void handleUnknownCarrierMovementId(UnknownCarrierMovementIdException e) {
88
+    logger.info("Placing event in retry queue due to: " + e.getMessage());
89
+  }
90
+
91
+  private void handleUnknownTrackingId(Exception e) {
65
     logger.info("Placing event in retry queue due to: " + e.getMessage());
92
     logger.info("Placing event in retry queue due to: " + e.getMessage());
66
-    // TODO: actually place in a retry queue
67
   }
93
   }
68
 
94
 
69
   private Date parseIso8601Date(final String completionTime) throws ParseException {
95
   private Date parseIso8601Date(final String completionTime) throws ParseException {