feat(linkDocument): add error handling
This commit is contained in:
parent
e8ed4df31a
commit
f620252406
|
@ -3,6 +3,7 @@ import { htmlToText } from 'html-to-text';
|
||||||
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
|
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
|
||||||
import { Document } from '@langchain/core/documents';
|
import { Document } from '@langchain/core/documents';
|
||||||
import pdfParse from 'pdf-parse';
|
import pdfParse from 'pdf-parse';
|
||||||
|
import logger from '../utils/logger';
|
||||||
|
|
||||||
export const getDocumentsFromLinks = async ({ links }: { links: string[] }) => {
|
export const getDocumentsFromLinks = async ({ links }: { links: string[] }) => {
|
||||||
const splitter = new RecursiveCharacterTextSplitter();
|
const splitter = new RecursiveCharacterTextSplitter();
|
||||||
|
@ -16,6 +17,7 @@ export const getDocumentsFromLinks = async ({ links }: { links: string[] }) => {
|
||||||
? link
|
? link
|
||||||
: `https://${link}`;
|
: `https://${link}`;
|
||||||
|
|
||||||
|
try {
|
||||||
const res = await axios.get(link, {
|
const res = await axios.get(link, {
|
||||||
responseType: 'arraybuffer',
|
responseType: 'arraybuffer',
|
||||||
});
|
});
|
||||||
|
@ -76,6 +78,20 @@ export const getDocumentsFromLinks = async ({ links }: { links: string[] }) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
docs.push(...linkDocs);
|
docs.push(...linkDocs);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(
|
||||||
|
`Error at generating documents from links: ${err.message}`,
|
||||||
|
);
|
||||||
|
docs.push(
|
||||||
|
new Document({
|
||||||
|
pageContent: `Failed to retrieve content from the link: ${err.message}`,
|
||||||
|
metadata: {
|
||||||
|
title: 'Failed to retrieve content',
|
||||||
|
url: link,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue