Shorten Codable error messages so they are not redundant with debug descriptions from stdlib#1770
Shorten Codable error messages so they are not redundant with debug descriptions from stdlib#1770ZevEisenberg wants to merge 1 commit intoswiftlang:mainfrom
Conversation
| throw DecodingError.typeMismatch([String: Any].self, DecodingError.Context( | ||
| codingPath: self.codingPath, | ||
| debugDescription: "Expected to decode \([String: Any].self) but found \(topValue.debugDataTypeDescription) instead." | ||
| )) |
There was a problem hiding this comment.
I'm pretty sure this code is only reachable in FOUNDATION_FRAMEWORK builds, but I couldn't say for sure. In any case, I was not able to reach it from the public interface of JSONDecoder, and I was hesitant to start writing tests for internal functions.
|
Oh, and: would a change like this require a Foundation evolution proposal, mirroring SE-0489? It’s changing the values produced by the decoders, but only the |
|
I know that where you said
You meant "Foundation provides the expected type a second time, as well as the actual type.", but I can't help but think that you were channeling the Department of Redundancy Department, which is, after all, the entire point of this PR! #redundant #redundant |
Fixed! And rebased for good measure. |
… debug descriptions in cases where DecodingError already provides the same information.
Shorten error messages from built-in encoders and decoders so they are not redundant with debug descriptions from
EncodingErrorandDecodingErrorin the stdlib.Motivation:
Consider the following error message:
Following changes from SE-0489, it is shortened to the following:
But we can do better. Note that there is still redundant information making the error message unnecessarily long. Let's break it down into pieces:
DecodingError.typeMismatch: expected value of type String. Path: [0].address.city.birds[1].name. Debug description:Swift.DecodingError.debugDescriptionExpected to decode String but found number insteadFoundation.JSONDecoder.Swift.DecodingError.debugDescriptionThe stdlib already says what kind of value we expected (a
String), but not what type was expected. Foundation provides the expected type a second time, as well as the actual found type.Modifications:
PlistDecoderGenericwas seemingly doing an invalid check foris any UnkeyedDecodingContainer- that check was failing on a test case which did not fail withJSONDecoder, so I modified the check inPlistDecoderGenericto match.Testing:
I added unit tests for the errors generated by decoders in Foundation. I'm not testing the full
debugDescriptionof the resulting error, since that's controlled by the stdlib, but that choice makes it hard to see the full impact of this change. In order to see the full change, capture the error thrown by the encoding/decoding failure and printString(reflecting: error). I'm wondering whether there's anything I can do to improve this setup?Open Questions:
JSONDecoderandPropertyListDecoderthat seem to be unreachable. Are they in fact unreachable?PlistDecoderGenericok?valueNotFounddebug description swift#87408? If so, it will slightly impact the strings we end up with in this PR. It was the initial investigation for this Foundation PR that led me to notice the discrepancy with null values in the stdlib.