Skip to content

Commit 05e793c

Browse files
netmap: don't panic in Price and Capacity methods (#747)
2 parents abf7dc3 + 8554080 commit 05e793c

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

netmap/node_info.go

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -230,19 +230,14 @@ func (x *NodeInfo) SetPrice(price uint64) {
230230
x.setNumericAttribute(attrPrice, price)
231231
}
232232

233-
// Price returns price set using SetPrice.
233+
// Price returns price stored in the Price node attribute.
234234
//
235-
// Zero NodeInfo has zero price.
235+
// Invalid (non-numeric, not valid for returned type) or missing attribute
236+
// yields zero in return value.
237+
//
238+
// See [NodeInfo.SetPrice] also.
236239
func (x NodeInfo) Price() uint64 {
237-
val := x.Attribute(attrPrice)
238-
if val == "" {
239-
return 0
240-
}
241-
242-
price, err := strconv.ParseUint(val, 10, 64)
243-
if err != nil {
244-
panic(fmt.Sprintf("unexpected price parsing error %s: %v", val, err))
245-
}
240+
price, _ := strconv.ParseUint(x.Attribute(attrPrice), 10, 64)
246241

247242
return price
248243
}
@@ -259,19 +254,14 @@ func (x *NodeInfo) SetVersion(version string) {
259254
x.SetAttribute(attrVersion, version)
260255
}
261256

262-
// Capacity returns capacity set using SetCapacity.
257+
// Capacity returns capacity (GB) stored in the Capacity node attribute.
263258
//
264-
// Zero NodeInfo has zero capacity.
259+
// Invalid (non-numeric, not valid for returned type) or missing attribute
260+
// yields zero in return value.
261+
//
262+
// See [NodeInfo.SetCapacity] also.
265263
func (x NodeInfo) Capacity() uint64 {
266-
val := x.Attribute(attrCapacity)
267-
if val == "" {
268-
return 0
269-
}
270-
271-
capacity, err := strconv.ParseUint(val, 10, 64)
272-
if err != nil {
273-
panic(fmt.Sprintf("unexpected capacity parsing error %s: %v", val, err))
274-
}
264+
capacity, _ := strconv.ParseUint(x.Attribute(attrCapacity), 10, 64)
275265

276266
return capacity
277267
}

0 commit comments

Comments
 (0)