Angular HttpErrorResponse blob error response doesn't trigger FileReader

Discussão em 'Angular' iniciado por dcp3450, Maio 20, 2025.

  1. dcp3450

    dcp3450 Guest

    When requesting a file and an error is returned I get the error response in a Blob instance

    error: Blob {size: 97, type: 'application/json'}
    headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
    message: "Http failure response for redacted: 404 OK"
    name: "HttpErrorResponse"
    ok: false
    status: 404
    statusText: "OK"
    url: "redacted"


    Given that, I can't do my standard check in the error property for a message. I'm attempting to read the response as the response itself is the correct format, just a blob:

    if (requestError.type === 'application/json') {
    console.log('start parsing');
    const reader = new FileReader();
    reader.onload = (event) => {
    console.log(event);
    console.log('reader.result', reader.result);
    try {
    requestError = JSON.parse(reader.result as string);
    console.log('Parsed JSON response:', requestError);
    } catch (e) {
    console.log('Error parsing JSON response:', e);
    }
    }
    return;
    }


    "start parsing" is fired but the onload event isn't. I've tried onloadend as well as setting the event trigger as reader.addEventListener('loadend', (event) => { but nothing inside the loading events fires. I'm checking the response I can confirm it's a blob and the response is in json.

    Not sure why the FileReader event's are being triggered.

    Continue reading...

Compartilhe esta Página