tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ResultExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using Remora.Discord.API.Abstractions.Objects;
6using Remora.Discord.API.Objects;
7using Remora.Rest.Results;
8using Remora.Results;
9
11{
15 static class ResultExtensions
16 {
23 public static string LogFormat(this IResult result, uint level = 0)
24 {
25 ArgumentNullException.ThrowIfNull(result);
26
27 if (result.IsSuccess)
28 return "SUCCESS?";
29
30 var stringBuilder = new StringBuilder();
31 if (result.Error != null)
32 {
33 stringBuilder.Append(result.Error.Message);
34 if (result.Error is RestResultError<RestError> restError)
35 {
36 stringBuilder.Append(" (");
37 if (restError.Error != null)
38 {
39 stringBuilder.Append(restError.Error.Code);
40 stringBuilder.Append(": ");
41 stringBuilder.Append(restError.Error.Message);
42 stringBuilder.Append('|');
43 }
44
45 stringBuilder.Append(restError.Message);
46 if ((restError.Error?.Errors.HasValue ?? false) && restError.Error.Errors.Value.Count > 0)
47 {
48 stringBuilder.Append(" (");
49 foreach (var error in restError.Error.Errors.Value)
50 {
51 stringBuilder.Append(error.Key);
52 stringBuilder.Append(':');
53 if (error.Value.IsT0)
54 {
55 FormatErrorDetails(error.Value.AsT0, stringBuilder);
56 }
57 else
58 FormatErrorDetails(error.Value.AsT1, stringBuilder);
59 stringBuilder.Append(',');
60 }
61
62 stringBuilder.Remove(stringBuilder.Length - 1, 1);
63 }
64
65 stringBuilder.Append(')');
66 }
67 }
68
69 if (result.Inner != null)
70 {
71 stringBuilder.Append(Environment.NewLine);
72 ++level;
73 for (var i = 0; i < level; ++i)
74 stringBuilder.Append('\t');
75 stringBuilder.Append(result.Inner.LogFormat(level));
76 }
77
78 return stringBuilder.ToString();
79 }
80
86 static void FormatErrorDetails(IPropertyErrorDetails propertyErrorDetails, StringBuilder stringBuilder)
87 {
88 if (propertyErrorDetails == null)
89 return;
90
91 FormatErrorDetails(propertyErrorDetails.Errors, stringBuilder);
92
93 if (propertyErrorDetails.Errors != null && propertyErrorDetails.MemberErrors != null)
94 {
95 stringBuilder.Append(',');
96 }
97
98 if (propertyErrorDetails.MemberErrors != null)
99 {
100 stringBuilder.Append('{');
101 foreach (var error in propertyErrorDetails.MemberErrors)
102 {
103 stringBuilder.Append(error.Key);
104 stringBuilder.Append(':');
105 FormatErrorDetails(error.Value, stringBuilder);
106 stringBuilder.Append(',');
107 }
108
109 stringBuilder.Remove(stringBuilder.Length - 1, 1);
110 stringBuilder.Append('}');
111 }
112 }
113
119 static void FormatErrorDetails(IEnumerable<IErrorDetails>? errorDetails, StringBuilder stringBuilder)
120 {
121 if (errorDetails == null)
122 return;
123
124 stringBuilder.Append('[');
125 foreach (var error in errorDetails)
126 {
127 stringBuilder.Append(error.Code);
128 stringBuilder.Append(':');
129 stringBuilder.Append(error.Message);
130 stringBuilder.Append(',');
131 }
132
133 stringBuilder.Remove(stringBuilder.Length - 1, 1);
134 stringBuilder.Append(']');
135 }
136 }
137}
static void FormatErrorDetails(IEnumerable< IErrorDetails >? errorDetails, StringBuilder stringBuilder)
Formats given errorDetails into a given stringBuilder .
static void FormatErrorDetails(IPropertyErrorDetails propertyErrorDetails, StringBuilder stringBuilder)
Formats given propertyErrorDetails into a given stringBuilder .
static string LogFormat(this IResult result, uint level=0)
Converts a given result into a log entry string.