Tampermonkey / tampermonkey

Tampermonkey is the most popular userscript manager, with over 10 million users. It's available for Chrome, Microsoft Edge, Safari, Opera Next, and Firefox.
GNU General Public License v3.0
4.17k stars 416 forks source link

`GM.xmlHttpRequest.onerror` causes uncaught exception when logging error #2158

Open adamlui opened 3 weeks ago

adamlui commented 3 weeks ago

(Please fill out the issue template with your details)

Expected Behavior

Log the error, do not throw

Actual Behavior

Uncaught TypeError: can't convert label to string

Specifications

Script

(Please give an example of the script if applicable.)

...
function consoleErr(label, msg) { console.error(`${config.appSymbol} ${config.appName} » ${label}${ msg ? `: ${msg}` : '' }`)}

GM.xmlHttpRequest({
    method: apis[get.reply.api].method,
    url: apis[get.reply.api].endpoints?.completions || apis[get.reply.api].endpoint,
    responseType: config.streamingDisabled || !config.proxyAPIenabled ? 'text' : 'stream',
    headers: api.createHeaders(get.reply.api), data: api.createPayload(get.reply.api, msgChain),
    onload: resp => dataProcess.text(get.reply, resp),
    onloadstart: resp => dataProcess.stream(get.reply, resp),
    onerror: err => { consoleErr(err)
        if (!config.proxyAPIenabled) appAlert(!config.openAIkey ? 'login' : ['openAInotWorking', 'suggestProxy'])
        else if (get.reply.status != 'done') api.tryNew(get.reply)
    }
})
...

The above on error will throw unless err is wrapped in JSON.stringify()

image

(In Violentmonkey it does not throw + properly logs the err obj and script healthily keeps on running)

image

Maybe related to https://github.com/Tampermonkey/tampermonkey/issues/1877

adamlui commented 3 weeks ago

I fixed the throwing by updating consoleErr() to:

function consoleErr(label, msg) {
    console.error( `${config.appSymbol} ${config.appName} » ${
        typeof label == 'object' ? JSON.stringify(label) : label }${ msg ? `: ${msg}` : ''}`)
}

... but it's still nice how VM auto-converts, also for some reason their obj is much more detailed

TM:

image

VM:

image

derjanb commented 2 weeks ago

Will be fixed at the next BETA version. Thanks for reporting.

Out of curiosity, the following test script is always logging [object Object] here. With no browser or script manager I get the stringified object logged.

image

// ==UserScript==
// @name         234234.example.com
// @namespace    http://tampermonkey.net/
// @version      2024-08-29
// @description  try to take over the world!
// @author       You
// @match        https://*/*
// @grant        GM.xmlHttpRequest
// @connect      example.com
// ==/UserScript==

function consoleErr(label, msg) { console.error(`aa bb » ${label}${ msg ? `: ${msg}` : '' }`)}

GM.xmlHttpRequest({
    method: "GET",
    url: "https://234234.example.com",
    onerror: function(e) {
      consoleErr(e);
    }
});
adamlui commented 2 weeks ago

What's no browser or script manager mean? Also the code in FF TM shows the original TypeError to me

derjanb commented 2 weeks ago

You said "in Violentmonkey it does not throw + properly logs the err obj". What does "properly" mean? I only get the string [object Object] being logged.

adamlui commented 1 week ago

You said "in Violentmonkey it does not throw + properly logs the err obj". What does "properly" mean? I only get the string [object Object] being logged.

I get the pic I pasted 358860236-b6c362bd-56c8-47e8-b191-60994e3483e7

Also my code is:

                onerror: err => { log.err(err)
                    if (!config.proxyAPIenabled) appAlert(!config.openAIkey ? 'login' : ['openAInotWorking', 'suggestProxy'])
                    else if (get.reply.status != 'done') api.tryNew(get.reply)
                }

... and I notice on error, it logs only, the next 2 lines don't run

image

When I simplify the onerror to just api.tryNew(get.reply) it works though

adamlui commented 1 week ago

I figured it out, the stream is considered done by the reader on error so i removed the condition

derjanb commented 1 week ago

Should be fixed at 5.3.6209 (crx|xpi in review)

Please download the crx file linked above and drag and drop it to the extensions page chrome://extensions (after you've enabled 'Developer Mode').

For a quick fix please export your settings and scripts as zip or (JSON) file at the "Utilities" tab and import it back at the fixed BETA version.