use try catch

This commit is contained in:
SrGooglo 2023-03-06 02:08:14 +00:00
parent 0af07301f4
commit 974733bc5f

View File

@ -5,44 +5,50 @@ import isNSFW from "../../../utils/is-nsfw"
import modifyPostData from "./modifyPostData" import modifyPostData from "./modifyPostData"
export default async (post_id) => { export default async (post_id) => {
if (!post_id) { try {
throw new Error("Post ID is required") if (!post_id) {
} throw new Error("Post ID is required")
}
let post = await Post.findById(post_id) let post = await Post.findById(post_id)
if (!post) { if (!post) {
throw new Error("Post not found") throw new Error("Post not found")
} }
let flags = [] let flags = []
// run indecentPrediction to all attachments // run indecentPrediction to all attachments
if (Array.isArray(post.attachments) && post.attachments.length > 0) { if (Array.isArray(post.attachments) && post.attachments.length > 0) {
for await (const attachment of post.attachments) { for await (const attachment of post.attachments) {
const prediction = await indecentPrediction({ const prediction = await indecentPrediction({
url: attachment.url, url: attachment.url,
}).catch((err) => { }).catch((err) => {
console.log("Error while checking", attachment, err) console.log("Error while checking", attachment, err)
return null return null
}) })
if (prediction) { if (prediction) {
const isNsfw = isNSFW(prediction) const isNsfw = isNSFW(prediction)
if (isNsfw) { if (isNsfw) {
flags.push("nsfw") flags.push("nsfw")
}
} }
} }
} }
}
// if is there new flags update post // if is there new flags update post
if (post.flags !== flags) { if (post.flags !== flags) {
await modifyPostData(post_id, { await modifyPostData(post_id, {
flags: flags, flags: flags,
}) })
} }
return flags return flags
} catch (error) {
console.error(error)
return []
}
} }