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

@ -6,8 +6,7 @@ namespace FunctionalTests;
public class TaskDetails {
JsonElement _e;
public TaskDetails() { }
readonly JsonElement _e;
public TaskDetails(JsonElement e) => _e = e;
public string Type => _e.GetStringProperty("type");
@ -77,22 +76,20 @@ public class TaskDetails {
public class TaskConfig : IFromJsonElement<TaskConfig> {
JsonElement _e;
public TaskConfig() { }
readonly JsonElement _e;
public TaskConfig(JsonElement e) => _e = e;
public TaskConfig Convert(JsonElement e) => new TaskConfig(e);
public static TaskConfig Convert(JsonElement e) => new(e);
public Guid JobId => _e.GetGuidProperty("job_id");
public IEnumerable<Guid>? PrereqTasks => _e.GetEnumerableGuidProperty("prereq_tasks");
}
public class OneFuzzTask : IFromJsonElement<OneFuzzTask> {
JsonElement _e;
readonly JsonElement _e;
public OneFuzzTask() { }
public OneFuzzTask(JsonElement e) => _e = e;
public OneFuzzTask Convert(JsonElement e) => new OneFuzzTask(e);
public static OneFuzzTask Convert(JsonElement e) => new(e);
public Guid JobId => _e.GetGuidProperty("job_id");
public Guid TaskId => _e.GetGuidProperty("task_id");