I have a middleware that handles the errors. It works as supposed to, including hte feature of correlation ID letting me communicate to the browser a GUID that I also print in the logs to quickly locate the exception.
public class ExceptionWare(RequestDelegate Next, ILogger<ExceptionWare> Logger)
{
public async Task InvokeAsync(HttpContext http)
{
try { await Next(http); }
catch (HttpException exception)
{
Guid corrId = Guid.NewGuid();
Logger.LogError(exception, ErrorTemplate, corrId);
http.Response.StatusCode = exception.StatusCode;
await http.Response.WriteAsync($"corrId: {corrId}");
}
catch (Exception exception) { ... }
}
}
The thing is that the correlation ID is delivered in the body of the response (purple arrow). I'd like to have it as a field of the error object delivered to my Angular application (yellow arrow). In there, I'm reading the status (set in the middy above), the message (set automagically somehow in my response) and some other fields.
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
return next.handle(request).pipe(
catchError((err: HttpErrorResponse) => {
console.error(err.corrId); // this is my desire
console.log(err.status);
console.log(err.message);
throw err;
}));
}
}
I'd like to amend that with corrId for ease of use without reading from the body. Right now, in my browser, I have to click around in the XHR response and pick the body tag.
After a few hours of googling, all I found was immense amounts of articles on altering the headers and/or writing to the response body. The former is of no interest to me and the latter is what I'm trying to improve.
I do carry a slight but growing feat that it's simply not achievable due to the limitation of the XHR object delivered to the browser. However, googling that angle, I haven't found anything at all that would indicate either way.
