Skip to content

Commit d155b09

Browse files
committed
adding sdk hint to error message response and powershell sample setup script
1 parent 778d2cd commit d155b09

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

msgraph-chaos-proxy/ChaosEngine.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class ChaosEngine {
7272
private ProxyServer? _proxyServer;
7373
private ExplicitProxyEndPoint? _explicitEndPoint;
7474
private readonly Dictionary<string, DateTime> _throttledRequests;
75+
private readonly ConsoleColor _color;
7576

7677
public ChaosEngine(ChaosProxyConfiguration config) {
7778
_config = config ?? throw new ArgumentNullException(nameof(config));
@@ -84,6 +85,8 @@ public ChaosEngine(ChaosProxyConfiguration config) {
8485
_methodStatusCode[k] = _methodStatusCode[k].Where(e => _config.AllowedErrors.Any(a => (int)e == a)).ToArray();
8586
}
8687
}
88+
89+
_color = Console.ForegroundColor;
8790
}
8891

8992
public async Task Run(CancellationToken? cancellationToken) {
@@ -214,16 +217,17 @@ private void HandleGraphRequest(SessionEventArgs e) {
214217

215218
FailResponse(e, responseComponents, failMode);
216219
if (!IsSdkRequest(e.HttpClient.Request)) {
217-
var color = Console.ForegroundColor;
218220
Console.ForegroundColor = ConsoleColor.Green;
219-
Console.Error.WriteLine($"\tTIP: To handle API errors more easily, use the Graph SDK. More info at {GetMoveToSdkUrl(e.HttpClient.Request)}");
220-
Console.ForegroundColor = color;
221+
Console.Error.WriteLine($"\tTIP: {BuildUseSdkMessage(e.HttpClient.Request)}");
222+
Console.ForegroundColor = _color;
221223
}
222224
}
223225
if (!responseComponents.ResponseIsComplete)
224226
UpdateProxyResponse(e, responseComponents, matchingResponse);
225227
}
226228

229+
private static string BuildUseSdkMessage(Request r) => $"To handle API errors more easily, use the Graph SDK. More info at {GetMoveToSdkUrl(r)}";
230+
227231
private void FailResponse(SessionEventArgs e, ResponseComponents r, FailMode failMode) {
228232
if (failMode == FailMode.Throttled) {
229233
r.ErrorStatus = HttpStatusCode.TooManyRequests;
@@ -327,7 +331,7 @@ private void UpdateProxyResponse(SessionEventArgs e, ResponseComponents response
327331
responseComponents.Body = JsonSerializer.Serialize(new ErrorResponseBody(
328332
new ErrorResponseError {
329333
Code = new Regex("([A-Z])").Replace(responseComponents.ErrorStatus.ToString(), m => { return $" {m.Groups[1]}"; }).Trim(),
330-
Message = "Some error happened",
334+
Message = BuildApiErrorMessage(e.HttpClient.Request),
331335
InnerError = new ErrorResponseInnerError {
332336
RequestId = responseComponents.RequestId,
333337
Date = responseComponents.RequestDate
@@ -339,6 +343,8 @@ private void UpdateProxyResponse(SessionEventArgs e, ResponseComponents response
339343
e.GenericResponse(responseComponents.Body ?? string.Empty, responseComponents.ErrorStatus, responseComponents.Headers);
340344
}
341345

346+
private string BuildApiErrorMessage(Request r) => $"Some error was generated by the proxy. {(IsSdkRequest(r) ? "" : BuildUseSdkMessage(r))}";
347+
342348
private string BuildThrottleKey(Request r) => $"{r.Method}-{r.Url}";
343349

344350
// Modify response

samples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dist
22
.parcel-cache
33
env.js
4+
env_esm.js

samples/readme.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ We highly recommend that you use a Microsoft 365 Developer Tenant with content p
1818

1919
## <a id="appreg">Configure Azure AD App Registration</a>
2020

21-
There are two ways which you can configur the App Registration required for the samples to work correctly, through automatation using a bash script or manually through Azure Portal.
21+
There are two ways which you can configure the App Registration required for the samples to work correctly, through automatation using a bash script or manually through Azure Portal.
2222

2323
### <a id="script">Script</a>
2424

25+
If using PowerShell:
26+
```powershell
27+
.\scripts\setup.ps1
28+
```
29+
30+
If using a Linux/Mac terminal:
2531
```shell
2632
$ > chmod +x /scripts/setup.sh
2733
$ > ./scripts/setup.sh

samples/scripts/setup.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# login
2+
Write-Output "Sign in to Microsoft 365..."
3+
npx -p @pnp/cli-microsoft365 -- m365 login --authType browser
4+
5+
# create AAD app
6+
Write-Output "Creating AAD app..."
7+
$appId = npx -p @pnp/cli-microsoft365 -- "m365 aad app add --name Graph-Chaos-Proxy-Samples --multitenant --redirectUris http://localhost:5500/withsdk.html,http://localhost:5500/nosdk.html --apisDelegated https://graph.microsoft.com/User.Read.All,https://graph.microsoft.com/Presence.Read.All --platform spa --query appId -o text"
8+
9+
Write-Output "AppId: $appId"
10+
11+
# write app to env.js
12+
Write-Output "Writing app to env.js..."
13+
"const appId = '$appId';" | Out-File .\env.js
14+
15+
# write app to env_esm.js
16+
"export const appId = '$appId';" | Out-File .\env_esm.js
17+
18+
Write-Output "DONE"

0 commit comments

Comments
 (0)