Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Improve messages in request.pushErrorMessage

Ondra Urban01667a854ab7
diff --git a/src/request.js b/src/request.js
index 304083906..e078ed6de 100644
--- a/src/request.js
+++ b/src/request.js
@@ -1,6 +1,5 @@
import { checkParamOrThrow } from 'apify-client/build/utils';
import { normalizeUrl } from 'apify-shared/utilities';
-import _ from 'underscore';
export const computeUniqueKey = (url, keepUrlFragment) => normalizeUrl(url, keepUrlFragment);
@@ -146,19 +145,47 @@ class Request {
/**
* Stores information about an error that occurred during processing of this request.
*
+ * You should always use Error instances when throwing errors in JavaScript.
+ *
+ * Nevertheless, to improve the debugging experience when using third party libraries
+ * that may not always throw an Error instance, the function performs a type
+ * inspection of the passed argument and attempts to extract as much information
+ * as possible, since just throwing a bad type error makes any debugging rather difficult.
+ *
* @param {Error|String} errorOrMessage Error object or error message to be stored in the request.
*/
pushErrorMessage(errorOrMessage) {
- if (!_.isString(errorOrMessage) && !(errorOrMessage instanceof Error)) {
- throw new Error('Parameter errorOrMessage must be a String or an instance of Error');
+ let message;
+ const type = typeof errorOrMessage;
+ switch (type) {
+ case 'undefined':
+ case 'boolean':
+ case 'number':
+ case 'function':
+ message = `Received: "${errorOrMessage}" of type: "${type}" instead of a proper message.`; break;
+ case 'string':
+ message = errorOrMessage; break;
+ case 'symbol':
+ message = errorOrMessage.toString(); break;
+ case 'object':
+ if (!errorOrMessage) {
+ message = 'Received: "null" instead of a proper message.';
+ } else if (errorOrMessage.message) {
+ message = errorOrMessage.message; // eslint-disable-line prefer-destructuring
+ } else {
+ try {
+ message = JSON.stringify(errorOrMessage, null, 2);
+ } catch (err) {
+ const keys = Object.keys(errorOrMessage).join('; ');
+ message = `Received an Object that is not stringifiable to JSON and has the following keys: ${keys}`;
+ }
+ }
+ break;
+ default:
+ message = 'Unable to extract any message from the received error.';
}
- const message = errorOrMessage instanceof Error
- ? errorOrMessage.message
- : errorOrMessage;
-
if (!this.errorMessages) this.errorMessages = [];
-
this.errorMessages.push(message);
}
}
diff --git a/test/request.js b/test/request.js
index 68d35d336..99973d7e6 100644
--- a/test/request.js
+++ b/test/request.js
@@ -22,10 +22,43 @@ describe('Apify.Request', () => {
expect(request.errorMessages).to.be.eql(null);
- request.pushErrorMessage(new Error('foo'));
+ // Make a circular, unstringifiable object.
+ const circularObj = { prop: 1 };
+ circularObj.obj = circularObj;
+
+ const arr = [1, 2, 3];
+ const arrJson = JSON.stringify(arr, null, 2);
+
+ const obj = { one: 1, two: 'two' };
+ const objJson = JSON.stringify(obj, null, 2);
+
+ request.pushErrorMessage(undefined);
+ request.pushErrorMessage(false);
+ request.pushErrorMessage(5);
+ request.pushErrorMessage(() => 2);
request.pushErrorMessage('bar');
+ request.pushErrorMessage(Symbol('A Symbol'));
+ request.pushErrorMessage(null);
+ request.pushErrorMessage(new Error('foo'));
+ request.pushErrorMessage({ message: 'A message.' });
+ request.pushErrorMessage([1, 2, 3]);
+ request.pushErrorMessage(obj);
+ request.pushErrorMessage(circularObj);
- expect(request.errorMessages).to.be.eql(['foo', 'bar']);
+ expect(request.errorMessages).to.be.eql([
+ 'Received: "undefined" of type: "undefined" instead of a proper message.',
+ 'Received: "false" of type: "boolean" instead of a proper message.',
+ 'Received: "5" of type: "number" instead of a proper message.',
+ 'Received: "() => 2" of type: "function" instead of a proper message.',
+ 'bar',
+ 'Symbol(A Symbol)',
+ 'Received: "null" instead of a proper message.',
+ 'foo',
+ 'A message.',
+ arrJson,
+ objJson,
+ 'Received an Object that is not stringifiable to JSON and has the following keys: prop; obj',
+ ]);
});
it('should should allow to have a GET request with payload', () => {