Skip to content

Commit

Permalink
fix: handle error message
Browse files Browse the repository at this point in the history
  • Loading branch information
LarchLiu committed May 14, 2023
1 parent 3ea34cf commit a74a0e9
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 65 deletions.
8 changes: 5 additions & 3 deletions app/bot/telegram-cl/src/message.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-prototype-builtins */
import { errorMessage } from '@starnexus/core'
import { CONST, DATABASE, ENV } from './env.js'
import { Context } from './context.js'
import { sendChatActionToTelegramWithContext, sendMessageToTelegramWithContext } from './telegram.js'
Expand Down Expand Up @@ -245,11 +246,12 @@ async function msgChatWithOpenAI(message, context) {
try {
// console.log(`Ask:${message.text}` || '')
setTimeout(() => sendChatActionToTelegramWithContext(context)('typing').catch(console.error), 0)
await saveToNotion(message.text)
return sendMessageToTelegramWithContext(context)('Saved to Notion 🎉')
const res = await saveToNotion(message.text)
return sendMessageToTelegramWithContext(context)(`Saved to StarNexus 🎉.\n${res}`)
}
catch (e) {
return sendMessageToTelegramWithContext(context)(`Error: ${e.message}`)
const message = errorMessage(e)
return sendMessageToTelegramWithContext(context)(`Error: ${message}`)
}
}

Expand Down
19 changes: 16 additions & 3 deletions app/bot/telegram-cl/src/notion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import { ENV } from './env.js'
* Given a URL, returns information about the website if it is hosted on GitHub.
*
* @param {string} text - The text input by user.
* @return {Promise<FetchWebsite[]>} - A Promise that resolves to the fetched information.
* @return {Promise<string>} - A Promise that resolves to the fetched information.
*/
async function saveToNotion(text) {
const regex = /(http(s)?:\/\/)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:[0-9]{1,5})?[-a-zA-Z0-9()@:%_\\\+\.~#?&//=]*/g
const match = text.match(regex)
if (match) {
for (let i = 0; i < match.length; i++) {
let i = 0
let success = 0
let fail = 0
while (i < match.length) {
const notionApiKey = ENV.NOTION_API_KEY
const databaseId = ENV.NOTION_DATABASE_ID
const openaiApiKey = ENV.API_KEY
Expand Down Expand Up @@ -41,8 +44,18 @@ async function saveToNotion(text) {
dataStorage: notion,
})

await chain.call()
const info = await chain.call().then(_ => true).catch(_ => false)

i++
if (info)
success++
else
fail++
}
return `Success: ${success}${fail ? (` Fail: ${fail}`) : ''}`
}
else {
throw new Error('No Website Matched')
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/bot/telegram-cl/src/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ interface WebInfoData {

interface GithubRepoMeta {
host: string
website: 'Github'
siteName: 'Github'
tags?: string[]
languages?: string[]
cover?: string
Expand Down
6 changes: 2 additions & 4 deletions app/extension/chrome/src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NotionStorage, SaveWebInfoChain, SummarizeContent, WebCard, WebInfoByApi } from '@starnexus/core'
import { NotionStorage, SaveWebInfoChain, SummarizeContent, WebCard, WebInfoByApi, errorMessage } from '@starnexus/core'
import type { ContentRequest, ListenerSendResponse, PageInfo, SwResponse } from '~/types'

async function sendSavedStatus(res: SwResponse) {
Expand Down Expand Up @@ -120,9 +120,7 @@ async function saveToNotion(pageInfo: PageInfo): Promise<SwResponse> {
return { tabId: pageInfo.tabId, starred: info.starred, notionPageId: info.notionPageId }
}
catch(error: any) {
let message = error.message || ''
if (error.data)
message = JSON.stringify(error.data)
const message = errorMessage(error)
return { tabId: pageInfo.tabId, starred: pageInfo.starred, notionPageId: pageInfo.notionPageId, error: message }
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/notion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export async function saveToNotion(config: NotionConfig, info: NotionPage): Prom
...body.properties,
Website: {
select: {
name: meta.website,
name: meta.siteName,
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface WebCardData {

export interface WebsiteMeta {
domain?: string
website?: string
siteName?: string
cover?: string
prompts?: string
}
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,20 @@ function replaceHtmlReservedCharacters(str: string) {
return escapeHTML(str)
}

function errorMessage(error: any) {
let message = error.message || ''
if (error.data)
message += JSON.stringify(error.data)

return message
}

export {
getDomain,
countWord,
preprocessText,
getPromptsByTemplate,
strNotEqualWith,
replaceHtmlReservedCharacters,
errorMessage,
}
2 changes: 1 addition & 1 deletion packages/core/src/webInfo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class WebInfo {
if (loaderUrls) {
const info = await pathInfo.loader(loaderUrls, this.headers)
info.meta.domain = domain
info.meta.website = router.name
info.meta.siteName = router.name

return info
}
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/webInfo/ogInfo.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import type { WebInfoData } from '../types'

export type OGInfoFun = () => Promise< WebInfoData>
export type OGInfoFun = (url: string) => Promise< WebInfoData>

export class OGInfo {
constructor(fun: OGInfoFun) {
this.fun = fun
constructor(fields: { fun: OGInfoFun; url: string }) {
this.fun = fields.fun
this.url = fields.url
}

private fun
private url

async call() {
return await this.fun()
return await this.fun(this.url)
}
}
2 changes: 1 addition & 1 deletion packages/core/test/notion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const notionPage = {
url: 'https://github.com/LarchLiu/star-nexus',
meta: {
domain: 'github.com',
website: 'Github',
siteName: 'Github',
cover: 'https://kiafhufrshqyrvlpsdqg.supabase.co/storage/v1/object/public/pics-bed/star-nexus.png?v=3',
},
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/openai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const webInfo: WebInfoData = {
url: 'https://github.com/LarchLiu/star-nexus',
meta: {
domain: '',
website: '',
siteName: '',
prompts: 'The Github repo info',
},
}
Expand Down
5 changes: 2 additions & 3 deletions packages/webHub/src/website/twitter.com/twitterApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { errorMessage } from '@starnexus/core'
import { $fetch } from 'ofetch'

// https://github.com/mikf/gallery-dl/blob/a53cfc845e12d9e98fefd07e43ebffaec488c18f/gallery_dl/extractor/twitter.py#L716-L726
Expand Down Expand Up @@ -151,9 +152,7 @@ export async function getTweetByStatus(status: string) {
return gatherLegacyFromData(tweets, 'none')
}
catch (error: any) {
let message = error.message || ''
if (error.data)
message = JSON.stringify(error.data)
const message = errorMessage(error)
throw new Error(`Twitter API error: ${message || error.statusText}`)
}
}
16 changes: 8 additions & 8 deletions server/nuxt3/server/api/webcard.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { unfurl } from 'unfurl.js'
import { $fetch } from 'ofetch'
import { createClient } from '@supabase/supabase-js'
import type { GithubRepoMeta, TwitterTweetMeta, WebInfoData } from '@starnexus/core'
import { errorMessage } from '@starnexus/core'
import type { Component } from 'vue'
import { satori } from '../utils/WebCard/satori'
import { initBaseFonts, loadDynamicAsset } from '../utils/WebCard/font'
Expand All @@ -25,13 +26,13 @@ export default eventHandler(async (event) => {
let png
const res = await unfurl(webInfo.url)

if (webMeta.website === 'Github') {
if (webMeta.siteName === 'Github') {
meta = webMeta as GithubRepoMeta
imgPath = `${webMeta.domain}/${meta.username}/${meta.reponame}.png`
if (res.open_graph && res.open_graph.images)
png = await $fetch(res.open_graph.images[0].url, { responseType: 'blob' })
}
else if (webMeta.website === 'Twitter') {
else if (webMeta.siteName === 'Twitter') {
card = TweetCard
meta = webMeta as TwitterTweetMeta
const screenName = meta.screenName!
Expand Down Expand Up @@ -62,7 +63,8 @@ export default eventHandler(async (event) => {
if (contentArr.length > 7)
contentArr = contentArr.slice(0, 8)

const favicon = (res.favicon && !res.favicon.includes('.ico')) ? res.favicon : ''
const faviconPath = res.favicon?.split('/')
const favicon = (faviconPath && !faviconPath[faviconPath.length - 1].includes('.ico')) ? res.favicon : ''
props = {
title: res.title || webInfo.title,
content: contentArr,
Expand All @@ -74,7 +76,7 @@ export default eventHandler(async (event) => {
}

if (!imgPath)
throw new Error(`No image path for ${webMeta.website}`)
throw new Error(`No image path for ${webMeta.siteName}`)

// const storageResponse = await $fetch(`${STORAGE_URL}/${imgPath}?v=starnexus`)
// .then((_) => {
Expand All @@ -90,7 +92,7 @@ export default eventHandler(async (event) => {
// }
if (!png) {
if (!card)
throw new Error(`No WebCard template for ${webMeta.website}`)
throw new Error(`No WebCard template for ${webMeta.siteName}`)

const fonts = await initBaseFonts()
svg = await satori(card, {
Expand Down Expand Up @@ -154,9 +156,7 @@ export default eventHandler(async (event) => {
catch (error: any) {
setResponseStatus(event, 400)

let message = error.message || ''
if (error.data)
message = JSON.stringify(error.data)
const message = errorMessage(error)
return { error: message }
}
})
38 changes: 4 additions & 34 deletions server/nuxt3/server/api/webinfo.post.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,20 @@
import type { WebInfoData } from '@starnexus/core'
import { OGInfo, WebInfo, getDomain } from '@starnexus/core'
import { OGInfo, WebInfo, errorMessage } from '@starnexus/core'
import { routes } from '@starnexus/web-hub'
import { unfurl } from 'unfurl.js'
import { ogInfoFun } from '../utils'

export default eventHandler(async (event) => {
const req = await readBody<{ webUrl: string }>(event)
const webUrl = req.webUrl
const ogInfoFun = async () => {
const res = await unfurl(webUrl)
let content = res.description || ''
let title = res.title || ''
let url = res.canonical_url || webUrl
let website = ''
const domain = getDomain(url)
if (res.open_graph) {
const og = res.open_graph
title = og.title
content = og.description || content
url = og.url || url
website = og.site_name || domain.split('.')[0].toUpperCase()
}

if (!title || !content)
throw new Error('Not Supported Website. No OG info')

return {
title,
content,
url,
meta: {
domain,
website,
},
} as WebInfoData
}
const ogInfo = new OGInfo(ogInfoFun)
const ogInfo = new OGInfo({ fun: ogInfoFun, url: webUrl })
try {
const webInfo = new WebInfo({ urls: { webUrl }, routes, ogInfo })
const webData = await webInfo.call()
return webData
}
catch (error: any) {
setResponseStatus(event, 400)
let message = error.message || ''
if (error.data)
message = JSON.stringify(error.data)
const message = errorMessage(error)
return { error: message }
}
})
33 changes: 33 additions & 0 deletions server/nuxt3/server/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { unfurl } from 'unfurl.js'
import type { WebInfoData } from '@starnexus/core'
import { getDomain } from '@starnexus/core'

export async function ogInfoFun(webUrl: string) {
const res = await unfurl(webUrl)
let content = res.description || ''
let title = res.title || ''
let url = res.canonical_url || webUrl
const domain = getDomain(url)
const word = domain.split('.')[0]
let siteName = word.charAt(0).toUpperCase() + word.slice(1)
if (res.open_graph) {
const og = res.open_graph
title = og.title
content = og.description || content
url = og.url || url
siteName = og.site_name || siteName
}

if (!title || !content)
throw new Error('Not Supported Website. No OG info')

return {
title,
content,
url,
meta: {
domain,
siteName,
},
} as WebInfoData
}

1 comment on commit a74a0e9

@vercel
Copy link

@vercel vercel bot commented on a74a0e9 May 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

star-nexus – ./

star-nexus.vercel.app
star-nexus-larchliu.vercel.app
star-nexus-git-main-larchliu.vercel.app

Please sign in to comment.