1# errors - errors as values.2#3# A function that can fail returns an error as its result, the caller checks4# it with failed():5#6# if failed(n = strconv.ParseInt("x", 10)) {7# return errors.Wrap(n, "parsing the port")8# }910# New builds an error with the given message.11New = fn(msg) { error(msg) }1213# Wrap returns a new error adding context to err, keeping its message.14Wrap = fn(err, msg) { error("{msg}: {string(err)}") }1516# Is reports whether err is an error whose message contains target.17Is = fn(err, target) {18 if type(err) != "error" {19 return false20 }2122 msg = string(err)23 sub = string(target)24 maxAttempts = len(msg) - len(sub)2526 for i = 0; i <= maxAttempts; ++i {27 if sub == slice(msg, i, i + len(sub)) {28 return true29 }30 }31 return false32}3334# Message returns the text of err, or the empty string if err is not an error.35Message = fn(err) {36 if type(err) != "error" {37 return ""38 }39 return string(err)40}