Codable + Hashable Errors

We should have codable errors.

Simply have a "CodableError" protocol that conforms to "Codable", and "Error" with string variables "name" and "code". For additional, we can have a "HashableError" thats an extension of "CodableError", conforming to "Hashable".

 protocol CodableError : Error & Codable {
    var code : Int { get set }
    var name : String { get set }
} 

protocol HashableError : CodableError {}

OR

protocol CodableError : Error & Hashable & Codable {
    var code : Int { get set }
    var name : String { get set }
}

Could you expand a little bit? For instance, how would this be better than existing forms that are similar? (E.g. one can already write Error & Codable to mean types that conform to both protocols, and Error & Hashable, and so on.)

1 Like

I forgot about the "&" feature for protocols. yea you would want to use that. Anyone can write this but the revolution of swift (for distributed system interaction) should include certain code practices that are very general constructs. This will only help evolutionize how coders write code. Its also a web standard to return status codes with names. The localized description value for "Error" can just be the JSON encoded string.

ex . 404 (code which happens to be an int): Not Found (name which happens to be the choice of way to refer to the idea that a page was not found).

Now that I think about it, it can be CodableError or CodableStatus since thats the latter is more general than the former. Perhaps both except changing the "code" variable to "error" in CodableError.

I am a bit wary and confused on whether "code" should be an Int to be consistent with hashable or should it be a string.