Repository: microsoft/onefuzz
File: src/ApiService/ApiService/onefuzzlib/ProxyOperations.cs (C#)

Code before:
            if (!result.IsOk) {
                return await SetFailed(proxy, result.ErrorV);
            }

            var nsgConfig = config.ProxyNsgConfig;
            var result2 = await _context.NsgOperations.SetAllowedSources(nsg, nsgConfig);

            if (!result2.IsOk) {
                return await SetFailed(proxy, result2.ErrorV);
            }

            var result3 = await _context.VmOperations.Create(vm with { Nsg = nsg });

            if (!result3.IsOk) {
                return await SetFailed(proxy, result3.ErrorV);
            }
            var r = await Replace(proxy);
            if (!r.IsOk) {
                _logTracer.WithHttpStatus(r.ErrorV).Error($"Failed to save proxy {proxy.ProxyId:Tag:ProxyId}");
            }
            return proxy;
        }
    }

    private async System.Threading.Tasks.Task<Proxy> SetProvisionFailed(Proxy proxy, VirtualMachineInstanceView? instanceView) {
        var errors = GetErrors(proxy, instanceView).ToArray();
        return await SetFailed(proxy, new Error(ErrorCode.PROXY_FAILED, errors.ToList()));
    }

    private async Task<Proxy> SetFailed(Proxy proxy, Error error) {
        if (proxy.Error is not null) {
            return proxy;
        }

        _logTracer.Error($"vm failed: {proxy.Region:Tag:Region} -{error:Tag:Error}");
        await _context.Events.SendEvent(new EventProxyFailed(proxy.Region, proxy.ProxyId, error));
        return await SetState(proxy with { Error = error }, VmState.Stopping);
    }


    private static IEnumerable<string> GetErrors(Proxy proxy, VirtualMachineInstanceView? instanceView) {
        yield return "provisioning failed";
        if (instanceView is null) {
            yield break;
        }

        foreach (var status in instanceView.Statuses) {
            if (status.Level == StatusLevelTypes.Error) {
                yield return $"code:{status.Code} status:{status.DisplayStatus} message:{status.Message}";
            }
        }

Review comment (suggestion):
NIT: Don't need `ToArray` if you're going to `ToList` after
```suggestion
        var errors = GetErrors(proxy, instanceView);
```

Code after:
            if (!result.IsOk) {
                return await SetFailed(proxy, result.ErrorV);
            }

            var nsgConfig = config.ProxyNsgConfig;
            var result2 = await _context.NsgOperations.SetAllowedSources(nsg, nsgConfig);

            if (!result2.IsOk) {
                return await SetFailed(proxy, result2.ErrorV);
            }

            var result3 = await _context.VmOperations.Create(vm with { Nsg = nsg });

            if (!result3.IsOk) {
                return await SetFailed(proxy, result3.ErrorV);
            }
            var r = await Replace(proxy);
            if (!r.IsOk) {
                _logTracer.WithHttpStatus(r.ErrorV).Error($"Failed to save proxy {proxy.ProxyId:Tag:ProxyId}");
            }
            return proxy;
        }
    }

    private async System.Threading.Tasks.Task<Proxy> SetProvisionFailed(Proxy proxy, VirtualMachineInstanceView? instanceView) {
        var errors = GetErrors(proxy, instanceView);
        return await SetFailed(proxy, new Error(ErrorCode.PROXY_FAILED, errors.ToList()));
    }

    private async Task<Proxy> SetFailed(Proxy proxy, Error error) {
        if (proxy.Error is not null) {
            return proxy;
        }

        _logTracer.Error($"vm failed: {proxy.Region:Tag:Region} -{error:Tag:Error}");
        await _context.Events.SendEvent(new EventProxyFailed(proxy.Region, proxy.ProxyId, error));
        return await SetState(proxy with { Error = error }, VmState.Stopping);
    }


    private static IEnumerable<string> GetErrors(Proxy proxy, VirtualMachineInstanceView? instanceView) {
        yield return "provisioning failed";
        if (instanceView is null) {
            yield break;
        }

        foreach (var status in instanceView.Statuses) {
            if (status.Level == StatusLevelTypes.Error) {
                yield return $"code:{status.Code} status:{status.DisplayStatus} message:{status.Message}";
            }
        }