Our back-end code is written in C# within the .NET environment, targeting the 4.6.1 framework. Recently, our front-end was upgraded from Angular 4 to Angular 8. During this upgrade, webpack transitioned from version 2.3 to version 4.41 and typescript from version 2.2 to 3.2.4.
Despite these changes, the core code itself has remained unchanged.
C#:
public override Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
var reject = false;
var principal = actionContext.RequestContext.Principal as ClaimsPrincipal;
if (principal == null || !principal.Identity.IsAuthenticated || reject) {
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized,
new { error = "Unauthorized request for Impersonate Mode" },
actionContext.ControllerContext.Configuration.Formatters.JsonFormatter);
return Task.FromResult<object>(null);
}
return Task.FromResult<object>(null);
}
Typescript:
actionErrorResponseHandler(response: Response) {
if(response.status === 401){
if(response.text().includes("Impersonate")){
this.displayText = ImpersonateRejectText;
}
this.show();
}
}
(EDIT) Usage example:
setupPopUpModal(modalHeader: string, displayText: string){
this.accountService.accountShowPopUpModal()
.pipe(catchError((response: Response) => this.rejectModal.actionErrorResponseHandler(response)))
.subscribe((result: string) => {
if(result == "Done"){
this.modalHeader = modalHeader;
this.displayText = displayText;
this.messageBoxModal.show();
}
})
}
Previously, everything was functioning correctly. However, after the upgrade, an error message stating "e.text is not a function" started appearing.
Upon inspecting Chrome's developer tools, the response prior to the upgrade looked like this:
https://i.sstatic.net/Cu3Ve.png
And post-upgrade, it looked like this:
https://i.sstatic.net/SIbKH.png
The issue stems from the reference to the .text() function attempting to retrieve the body as a string, which no longer exists. The desired message can now be found in e.error instead of response.error, with Angular/Typescript syntax differing on how to access it.
I suspect that I need to approach building and parsing the response differently, but I have been unable to locate relevant documentation on this topic. Any assistance would be greatly appreciated.