Skip to content

Commit ce53675

Browse files
authored
Merge pull request #3 from ScrapeGraphAI/error-management-js
Implemented error handling in js-sdk
2 parents 6752c43 + aa27aa5 commit ce53675

File tree

8 files changed

+82
-91
lines changed

8 files changed

+82
-91
lines changed

scrapegraph-js/examples/getCredits_example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCredits } from "scrapegraph-sdk";
1+
import { getCredits } from 'scrapegraph-sdk';
22
import 'dotenv/config';
33

44
try {

scrapegraph-js/examples/getSmartScraperRequest_example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getSmartScraperRequest } from "scrapegraph-sdk";
1+
import { getSmartScraperRequest } from 'scrapegraph-sdk';
22
import 'dotenv/config';
33

44
try {

scrapegraph-js/examples/sendFeedback_example.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { sendFeedback } from "scrapegraph-sdk";
1+
import { sendFeedback } from 'scrapegraph-sdk';
22
import 'dotenv/config';
33

44
try {
55
const apiKey = process.env.SGAI_APIKEY;
6-
const requestId = "16a63a80-c87f-4cde-b005-e6c3ecda278b";
6+
const requestId = '16a63a80-c87f-4cde-b005-e6c3ecda278b';
77
const rating = 5;
8-
const feedbackMessage = "This is a test feedback message.";
8+
const feedbackMessage = 'This is a test feedback message.';
99

1010
const feedback_response = await sendFeedback(apiKey, requestId, rating, feedbackMessage);
1111
console.log(feedback_response);

scrapegraph-js/examples/smartScraper_example.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { smartScraper } from "scrapegraph-sdk";
1+
import { smartScraper } from 'scrapegraph-sdk';
22
import 'dotenv/config';
33

44
try {
55
const apiKey = process.env.SGAI_APIKEY;
6-
const url = "https://scrapegraphai.com";
7-
const prompt = "What does the company do?";
6+
const url = 'https://scrapegraphai.com';
7+
const prompt = 'What does the company do?';
88

99
const response = await smartScraper(apiKey, url, prompt);
1010

scrapegraph-js/src/credits.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
import axios from 'axios';
2+
import handleError from './utils/handleError.js';
23

34
/**
45
* Retrieve credits from the API.
56
*
67
* @param {string} apiKey - Your ScrapeGraph AI API key
78
* @returns {Promise<string>} Response from the API in JSON format
89
*/
9-
export async function credits(apiKey) {
10-
const endpoint = "https://api.scrapegraphai.com/v1/credits";
10+
export async function getCredits(apiKey) {
11+
const endpoint = 'https://api.scrapegraphai.com/v1/credits';
1112
const headers = {
12-
"accept": "application/json",
13-
"SGAI-APIKEY": apiKey
13+
'accept': 'application/json',
14+
'SGAI-APIKEY': apiKey
1415
};
1516

1617
try {
1718
const response = await axios.get(endpoint, { headers });
18-
return JSON.stringify(response.data);
19+
return response.data;
1920
} catch (error) {
20-
if (error.response) {
21-
return JSON.stringify({
22-
error: "HTTP error occurred",
23-
message: error.message,
24-
status_code: error.response.status
25-
});
26-
}
27-
return JSON.stringify({
28-
error: "An error occurred",
29-
message: error.message
30-
});
21+
handleError(error)
3122
}
3223
}

scrapegraph-js/src/feedback.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import axios from 'axios';
2+
import handleError from './utils/handleError.js';
23

34
/**
45
* Send feedback to the API.
56
*
67
* @param {string} apiKey - Your ScrapeGraph AI API key
78
* @param {string} requestId - The request ID associated with the feedback
89
* @param {number} rating - The rating score
9-
* @param {string} feedbackText - The feedback message to send
10+
* @param {string} feedbackText - Optional feedback message to send
1011
* @returns {Promise<string>} Response from the API in JSON format
1112
*/
12-
export async function feedback(apiKey, requestId, rating, feedbackText) {
13-
const endpoint = "https://api.scrapegraphai.com/v1/feedback";
13+
export async function sendFeedback(apiKey, requestId, rating, feedbackText = null) {
14+
const endpoint = 'https://api.scrapegraphai.com/v1/feedback';
1415
const headers = {
15-
"accept": "application/json",
16-
"SGAI-APIKEY": apiKey,
17-
"Content-Type": "application/json"
16+
'accept': 'application/json',
17+
'SGAI-APIKEY': apiKey,
18+
'Content-Type': 'application/json'
1819
};
1920

2021
const feedbackData = {
@@ -25,18 +26,8 @@ export async function feedback(apiKey, requestId, rating, feedbackText) {
2526

2627
try {
2728
const response = await axios.post(endpoint, feedbackData, { headers });
28-
return JSON.stringify(response.data);
29+
return response.data;
2930
} catch (error) {
30-
if (error.response) {
31-
return JSON.stringify({
32-
error: "HTTP error occurred",
33-
message: error.message,
34-
status_code: error.response.status
35-
});
36-
}
37-
return JSON.stringify({
38-
error: "An error occurred",
39-
message: error.message
40-
});
31+
handleError(error);
4132
}
4233
}

scrapegraph-js/src/smartScraper.js

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios';
2+
import handleError from './utils/handleError.js'
23

34
/**
45
* Scrape and extract structured data from a webpage using ScrapeGraph AI.
@@ -8,13 +9,14 @@ import axios from 'axios';
89
* @param {string} prompt - Natural language prompt describing what data to extract
910
* @param {Object} [schema] - Optional schema object defining the output structure
1011
* @returns {Promise<string>} Extracted data in JSON format matching the provided schema
12+
* @throws - Will throw an error in case of an HTTP failure.
1113
*/
1214
export async function smartScraper(apiKey, url, prompt, schema = null) {
13-
const endpoint = "https://api.scrapegraphai.com/v1/smartscraper";
15+
const endpoint = 'https://api.scrapegraphai.com/v1/smartscraper';
1416
const headers = {
15-
"accept": "application/json",
16-
"SGAI-APIKEY": apiKey,
17-
"Content-Type": "application/json"
17+
'accept': 'application/json',
18+
'SGAI-APIKEY': apiKey,
19+
'Content-Type': 'application/json'
1820
};
1921

2022
const payload = {
@@ -24,72 +26,39 @@ export async function smartScraper(apiKey, url, prompt, schema = null) {
2426

2527
if (schema) {
2628
payload.output_schema = {
27-
description: schema.title || "Schema",
28-
name: schema.title || "Schema",
29+
description: schema.title || 'Schema',
30+
name: schema.title || 'Schema',
2931
properties: schema.properties || {},
3032
required: schema.required || []
3133
};
3234
}
3335

3436
try {
3537
const response = await axios.post(endpoint, payload, { headers });
36-
return JSON.stringify(response.data);
38+
return response.data;
3739
} catch (error) {
38-
if (error.response) {
39-
if (error.response.status === 403) {
40-
return JSON.stringify({
41-
error: "Access forbidden (403)",
42-
message: "You do not have permission to access this resource."
43-
});
44-
}
45-
return JSON.stringify({
46-
error: "HTTP error occurred",
47-
message: error.message,
48-
status_code: error.response.status
49-
});
50-
}
51-
return JSON.stringify({
52-
error: "An error occurred",
53-
message: error.message
54-
});
40+
handleError(error)
5541
}
5642
}
5743

5844
/**
59-
* Retrieve the status or the result of a scraping request. It also allows you to see the result of old requests.
45+
* Retrieve the status or the result of a smartScraper request. It also allows you to see the result of old requests.
6046
*
6147
* @param {string} apiKey - Your ScrapeGraph AI API key
62-
* @param {string} requestId - The request ID associated with the feedback
48+
* @param {string} requestId - The request ID associated with the output of a smartScraper request.
6349
* @returns {Promise<string>} Information related to the status or result of a scraping request.
6450
*/
65-
export async function smartScraperInfo(apiKey, requestId) {
66-
const endpoint = "https://api.scrapegraphai.com/v1/smartscraper/" + requestId;
51+
export async function getSmartScraperRequest(apiKey, requestId) {
52+
const endpoint = 'https://api.scrapegraphai.com/v1/smartscraper/' + requestId;
6753
const headers = {
68-
"accept": "application/json",
69-
"SGAI-APIKEY": apiKey,
54+
'accept': 'application/json',
55+
'SGAI-APIKEY': apiKey,
7056
};
7157

7258
try {
7359
const response = await axios.get(endpoint, { headers });
74-
return JSON.stringify(response.data)
60+
return response.data;
7561
} catch (error) {
76-
if (error.response) {
77-
if (error.response.status === 403) {
78-
return JSON.stringify({
79-
error: "Access forbidden (403)",
80-
message: "You do not have permission to access this resource."
81-
});
82-
}
83-
return JSON.stringify({
84-
error: "HTTP error occurred",
85-
message: error.message,
86-
status_code: error.response.status
87-
});
88-
}
89-
return JSON.stringify({
90-
error: "An error occurred",
91-
message: error.message
92-
});
62+
handleError(error)
9363
}
94-
9564
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class HttpError extends Error {
2+
constructor(statusCode, title, detail) {
3+
super(HttpError.makeMessage(statusCode, title, detail));
4+
this.statusCode = statusCode;
5+
this.title = title;
6+
this.detail = detail;
7+
}
8+
9+
static makeMessage(statusCode, title, detail) {
10+
let message = '';
11+
12+
message += statusCode ? `${statusCode} - ` : '(unknown status code) - ';
13+
message += title ? `${title} - ` : '(unknown error message) - ';
14+
message += detail ? `${JSON.stringify(detail)}` : '(unknown error detail)';
15+
16+
return message;
17+
}
18+
}
19+
20+
class NetworkError extends Error {
21+
constructor(message) {
22+
super(message);
23+
}
24+
}
25+
26+
class UnexpectedError extends Error {
27+
constructor(message) {
28+
super(message);
29+
}
30+
}
31+
32+
export default function handleError(error) {
33+
if (error.response) {
34+
throw new HttpError(error.response.status, error.response.statusText, error.response.data.detail)
35+
} else if (error.request) {
36+
throw new NetworkError('Impossible to contact the server. Check your internet connection.');
37+
} else {
38+
throw new UnexpectedError(`${error.message}`);
39+
}
40+
}

0 commit comments

Comments
 (0)