Add Serialize/Deserialize for ErrorStatusCode #1346
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR #1180 added
ErrorStatusCode
andClientErrorStatusCode
types that represent HTTP status codes that are validated to be in the 400-599 and 400-499 ranges, respectively. These are itnended to be returned by user-defined error types in theirHttpResponseError
implementations.User-defined errors are typically implemented as a type that implements
Serialize
andJsonSchema
along withHttpResponseError
, which in turn requires that the type have aFrom<dropshot::HttpError>
conversion, used in the case of extractor and dropshot-internal errors. In order for the user-defined error'sHttpResponseError
implementation to have the same status as thedropshot::HttpError
from which it was constructed, it must contain that status code within the error type so that it may return it. However,ErrorStatusCode
does not implementSerialize
,Deserialize
, orJsonSchema
, so holding it in the user-defined error type breaks deriving those traits.One solution is to use
#[serde(skip)]
for theErrorStatusCode
field, which I showed in thecustom-error.rs
example. When the user-defined error type is only used in the server, this is sufficient, and it will simply omit the status when serializing. However, this prevents the user-defined error type from implementingDeserialize
, since the status code is never serialized. This means that generated client code cannot use the same type for the error response value as the server code, which is desirable in some cases (e.g. to use the samefmt::Display
implementation on both sides, etc). Also, in some cases, it may be useful to include a status code in the serialized body, such as when embedding an HTTP error returned by an external service which may not actually be the same as the response's status code (e.g. I might return a 500 or a 503 when a request to an upstream service returns a 4xx error).Therefore, this commit adds
Serialize
,Deserialize
, andJsonSchema
implementations for theErrorStatusCode
andClientErrorStatusCode
types. These implementations serialize and deserialize these types as au16
, and we generate a JSON Schema that represents them as integers with appropriate minimum and maximum value validation.