Skip to content
This repository was archived by the owner on Aug 17, 2019. It is now read-only.

Commit 39f0a20

Browse files
committed
Report key that is related to some error
1 parent 4129575 commit 39f0a20

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/main/scala/com/ckkloverdos/props/Props.scala

+17-17
Original file line numberDiff line numberDiff line change
@@ -44,127 +44,127 @@ class Props(val map: Map[String, String])(implicit conv: Converters = Converters
4444

4545
def getBoolean(key: String): Maybe[Boolean] = map.get(key) match {
4646
case Some(value)
47-
conv.convertToBoolean(value)
47+
handleKeyError(key, conv.convertToBoolean(value))
4848

4949
case None
5050
NoVal
5151
}
5252

5353
def getBooleanEx(key: String): Boolean = map.get(key) match {
5454
case Some(value)
55-
conv.convertToBooleanEx(value)
55+
handleKeyError(key, conv.convertToBooleanEx(value))
5656

5757
case None
5858
throw new IllegalArgumentException("Unknown key %s".format(key))
5959
}
6060

6161
def getByte(key: String): Maybe[Byte] = map.get(key) match {
6262
case Some(value)
63-
conv.convertToByte(value)
63+
handleKeyError(key, conv.convertToByte(value))
6464

6565
case None
6666
NoVal
6767
}
6868

6969
def getByteEx(key: String): Byte = map.get(key) match {
7070
case Some(value)
71-
conv.convertToByteEx(value)
71+
handleKeyError(key, conv.convertToByteEx(value))
7272

7373
case None
7474
throw new IllegalArgumentException("Unknown key %s".format(key))
7575
}
7676

7777
def getShort(key: String): Maybe[Short] = map.get(key) match {
7878
case Some(value)
79-
conv.convertToShort(value)
79+
handleKeyError(key, conv.convertToShort(value))
8080

8181
case None
8282
NoVal
8383
}
8484

8585
def getShortEx(key: String): Short = map.get(key) match {
8686
case Some(value)
87-
conv.convertToShortEx(value)
87+
handleKeyError(key, conv.convertToShortEx(value))
8888

8989
case None
9090
throw new IllegalArgumentException("Unknown key %s".format(key))
9191
}
9292

9393
def getInt(key: String): Maybe[Int] = map.get(key) match {
9494
case Some(value)
95-
conv.convertToInt(value)
95+
handleKeyError(key, conv.convertToInt(value))
9696

9797
case None
9898
NoVal
9999
}
100100

101101
def getIntEx(key: String): Int = map.get(key) match {
102102
case Some(value)
103-
conv.convertToIntEx(value)
103+
handleKeyError(key, conv.convertToIntEx(value))
104104

105105
case None
106106
throw new IllegalArgumentException("Unknown key %s".format(key))
107107
}
108108

109109
def getLong(key: String): Maybe[Long] = map.get(key) match {
110110
case Some(value)
111-
conv.convertToLong(value)
111+
handleKeyError(key, conv.convertToLong(value))
112112

113113
case None
114114
NoVal
115115
}
116116

117117
def getLongEx(key: String): Long = map.get(key) match {
118118
case Some(value)
119-
conv.convertToLongEx(value)
119+
handleKeyError(key, conv.convertToLongEx(value))
120120

121121
case None
122122
throw new IllegalArgumentException("Unknown key %s".format(key))
123123
}
124124

125125
def getDouble(key: String): Maybe[Double] = map.get(key) match {
126126
case Some(value)
127-
conv.convertToDouble(value)
127+
handleKeyError(key, conv.convertToDouble(value))
128128

129129
case None
130130
NoVal
131131
}
132132

133133
def getDoubleEx(key: String): Double = map.get(key) match {
134134
case Some(value)
135-
conv.convertToDoubleEx(value)
135+
handleKeyError(key, conv.convertToDoubleEx(value))
136136

137137
case None
138138
throw new IllegalArgumentException("Unknown key %s".format(key))
139139
}
140140

141141
def getFloat(key: String): Maybe[Float] = map.get(key) match {
142142
case Some(value)
143-
conv.convertToFloat(value)
143+
handleKeyError(key, conv.convertToFloat(value))
144144

145145
case None
146146
NoVal
147147
}
148148

149149
def getFloatEx(key: String): Float = map.get(key) match {
150150
case Some(value)
151-
conv.convertToFloatEx(value)
151+
handleKeyError(key, conv.convertToFloatEx(value))
152152

153153
case None
154154
throw new IllegalArgumentException("Unknown key %s".format(key))
155155
}
156156

157157
def getProps(key: String): Maybe[Props] = map.get(key) match {
158158
case Some(value)
159-
conv.convert[Props](value)
159+
handleKeyError(key, conv.convert[Props](value))
160160

161161
case None
162162
NoVal
163163
}
164164

165165
def getPropsEx(key: String): Props = map.get(key) match {
166166
case Some(value)
167-
conv.convertEx[Props](value)
167+
handleKeyError(key, conv.convertEx[Props](value))
168168

169169
case None
170170
throw new IllegalArgumentException("Unknown key %s".format(key))
@@ -173,7 +173,7 @@ class Props(val map: Map[String, String])(implicit conv: Converters = Converters
173173

174174
def getList(key: String, separatorRegex: String = "\\s*,\\s*") = map.get(key) match {
175175
case Some(value)
176-
value.split(separatorRegex).toList
176+
handleKeyError(key, value.split(separatorRegex).toList)
177177

178178
case None
179179
Nil

src/main/scala/com/ckkloverdos/props/PropsBase.scala

+8
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,12 @@ trait PropsBase[K, V, P <: PropsBase[K, V, P]] {
6262
def keysIterator: Iterator[K] = map.keysIterator
6363

6464
def isEmpty = size == 0
65+
66+
protected[this] def handleKeyError[A](key: String, f: A): A = {
67+
try f
68+
catch {
69+
case e: Throwable
70+
throw new Exception("For key %s".format(key), e)
71+
}
72+
}
6573
}

0 commit comments

Comments
 (0)