-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
39 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
shared/AppInsightsCore/src/JavaScriptSDK/ResponseHelpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { dumpObj } from "@nevware21/ts-utils"; | ||
import { _eInternalMessageId, eLoggingSeverity } from "../JavaScriptSDK.Enums/LoggingEnums"; | ||
import { IDiagnosticLogger } from "../JavaScriptSDK.Interfaces/IDiagnosticLogger"; | ||
import { IBackendResponse } from "../JavaScriptSDK.Interfaces/IXDomainRequest"; | ||
import { _throwInternal } from "./DiagnosticLogger"; | ||
import { getJSON } from "./EnvUtils"; | ||
|
||
/** | ||
* Parses the response from the backend. | ||
* @param response - XMLHttpRequest or XDomainRequest response | ||
*/ | ||
export function parseResponse(response: any, diagLog?: IDiagnosticLogger): IBackendResponse { | ||
try { | ||
if (response && response !== "") { | ||
const result = getJSON().parse(response); | ||
|
||
if (result && result.itemsReceived && result.itemsReceived >= result.itemsAccepted && | ||
result.itemsReceived - result.itemsAccepted === result.errors.length) { | ||
return result; | ||
} | ||
} | ||
} catch (e) { | ||
_throwInternal(diagLog, | ||
eLoggingSeverity.CRITICAL, | ||
_eInternalMessageId.InvalidBackendResponse, | ||
"Cannot parse the response. " + (e.name || dumpObj(e)), | ||
{ | ||
response | ||
}); | ||
} | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters