0

When I send an error message from my express nodejs app using res.status(400).send(err.stack);, I cannot seem to get out of the decoder stream I setup on the receiving end.

Here is my code in the browser (limited to the fetch portion):

fetch("/the/url", {
    method: "POST",
    body: formData,
}).then(response => {
    if (response.status === 200) {
        return response.blob().then((data) => {
            return data;
        });
    } else {
        return new Promise((resolve, reject) => {
            let err_message = "";
            let reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
            reader.read().then(({done, value}) => {
                // When no more data needs to be consumed, close the stream
                if (done) {
                    reject(err_message);
                    return;
                }
                // Enqueue the next data chunk into our target stream
                err_message += value;
            });
        }).then((res) => {
            return (res)
        }).catch((err) => {
            return (err)
        });
    }
}).then((res) => {
    console.log(res)
}).catch((err) => {
    console.log(err)
});

I have put breakpoints on all subsequent then and catch but it never resolves/rejects.

Appreciate any pointers.

1 Answer 1

0

In case it's helpful to someone, you need to make a recursive call to the same function to break out properly. As per the following :

fetch("/the/url", {
    method: "POST",
    body: formData,
}).then(response => {
    if (response.status === 200) {
        return response.blob().then((data) => {
            return data;
        });
    } else {
        return new Promise((resolve, reject) => {
            let err_message = "";
            let reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
            reader.read().then(function processText({done, value}) {
                // When no more data needs to be consumed, close the stream
                if (done) {
                    reject(err_message);
                    return;
                }
                // Enqueue the next data chunk into our target stream
                err_message += value;
                return reader.read().then(processText);
            });
        }).then((res) => {
            return (res)
        }).catch((err) => {
            return (err)
        });
    }
}).then((res) => {
    console.log(res)
}).catch((err) => {
    console.log(err)
});

from https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/getReader

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.