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.

Simplify type inspection and logging

Ondra Urbanb571c799f772
diff --git a/src/request.js b/src/request.js
index 18036bc8c..9ca20d8fd 100644
--- a/src/request.js
+++ b/src/request.js
@@ -1,3 +1,4 @@
+import util from 'util';
import { checkParamOrThrow } from 'apify-client/build/utils';
import { normalizeUrl } from 'apify-shared/utilities';
@@ -157,36 +158,24 @@ class Request {
pushErrorMessage(errorOrMessage) {
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 (type === 'object') {
if (!errorOrMessage) {
- message = 'Received: "null" instead of a proper message.';
- } else if (Array.isArray(errorOrMessage)) {
- message = `Received: "array" instead of a proper message.\nContents: ${errorOrMessage}`;
+ message = 'null';
} else if (errorOrMessage.message) {
message = errorOrMessage.message; // eslint-disable-line prefer-destructuring
} else if (errorOrMessage.toString() !== '[object Object]') {
message = errorOrMessage.toString();
} else {
try {
- message = JSON.stringify(errorOrMessage, null, 2);
+ message = util.inspect(errorOrMessage);
} catch (err) {
- const keys = Object.keys(errorOrMessage).join('; ');
- message = `Received an Object that is not stringifiable to JSON and has the following keys: ${keys}`;
+ message = 'Unable to extract any message from the received object.';
}
}
- break;
- default:
- message = 'Unable to extract any message from the received error.';
+ } else if (type === 'undefined') {
+ message = 'undefined';
+ } else {
+ message = errorOrMessage.toString();
}
if (!this.errorMessages) this.errorMessages = [];
diff --git a/test/request.js b/test/request.js
index 4ecf43800..707afa7d7 100644
--- a/test/request.js
+++ b/test/request.js
@@ -1,3 +1,4 @@
+import util from 'util';
import { expect } from 'chai';
import { computeUniqueKey } from '../build/request';
import Apify from '../build/index';
@@ -25,11 +26,10 @@ describe('Apify.Request', () => {
// Make a circular, unstringifiable object.
const circularObj = { prop: 1 };
circularObj.obj = circularObj;
-
- const arr = [1, 2, 3];
+ const circularObjInspect = util.inspect(circularObj);
const obj = { one: 1, two: 'two' };
- const objJson = JSON.stringify(obj, null, 2);
+ const objInspect = util.inspect(obj);
const toStr = {
toString() {
@@ -52,19 +52,19 @@ describe('Apify.Request', () => {
request.pushErrorMessage(circularObj);
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.',
+ 'undefined',
+ 'false',
+ '5',
+ '() => 2',
'bar',
'Symbol(A Symbol)',
- 'Received: "null" instead of a proper message.',
+ 'null',
'foo',
'A message.',
- `Received: "array" instead of a proper message.\nContents: ${arr}`,
- objJson,
+ '1,2,3',
+ objInspect,
'toString',
- 'Received an Object that is not stringifiable to JSON and has the following keys: prop; obj',
+ circularObjInspect,
]);
});