Standardize HTTP error results, better rejection message when parsing validated strings (#2663)

1. When parsing a `ValidatedString` from JSON and it fails, include a message about the expected format of the string.
   - Reworked the classes using C#11 features to reduce the amount of boilerplate needed to add a new validated string type.

2. Change to use [RFC7807](https://www.rfc-editor.org/rfc/rfc7807) format for HTTP error responses. At the moment we returned the `Error` type which was undocumented.
3. Update CLI to parse RFC7807 responses.

Old error looked like:

```console
$ onefuzz containers create AbCd
ERROR:cli:command failed: request did not succeed: HTTP 500 -
```

New error looks like:

```console
$ onefuzz containers create AbCd
ERROR:cli:command failed: request did not succeed (400: INVALID_REQUEST): Unable
to parse 'AbCd' as a Container: Container name must be 3-63 lowercase letters, numbers,
or non-consecutive hyphens (see: https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules#microsoftstorage)
```

Closes #2661.
This commit is contained in:
George Pollard
2022-12-02 10:33:14 +13:00
committed by GitHub
parent 88e8c11a02
commit 38dfa668bc
28 changed files with 304 additions and 294 deletions

View File

@ -34,8 +34,8 @@ public abstract class DownloadTestBase : FunctionTestBase {
var result = await func.Run(TestHttpRequestData.Empty("GET"));
Assert.Equal(HttpStatusCode.Unauthorized, result.StatusCode);
var err = BodyAs<Error>(result);
Assert.Equal(ErrorCode.UNAUTHORIZED, err.Code);
var err = BodyAs<ProblemDetails>(result);
Assert.Equal(ErrorCode.UNAUTHORIZED.ToString(), err.Title);
}
[Fact]
@ -49,8 +49,8 @@ public abstract class DownloadTestBase : FunctionTestBase {
var result = await func.Run(req);
Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode);
var err = BodyAs<Error>(result);
Assert.Equal(ErrorCode.INVALID_REQUEST, err.Code);
var err = BodyAs<ProblemDetails>(result);
Assert.Equal(ErrorCode.INVALID_REQUEST.ToString(), err.Title);
}
[Fact]
@ -65,8 +65,8 @@ public abstract class DownloadTestBase : FunctionTestBase {
var result = await func.Run(req);
Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode);
var err = BodyAs<Error>(result);
Assert.Equal(ErrorCode.INVALID_REQUEST, err.Code);
var err = BodyAs<ProblemDetails>(result);
Assert.Equal(ErrorCode.INVALID_REQUEST.ToString(), err.Title);
}
[Fact]