Skip to content

Commit

Permalink
fix: regex error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
paring-chan committed Apr 19, 2023
1 parent eccf634 commit 7c4698a
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 99 deletions.
21 changes: 21 additions & 0 deletions bot/findInvalidRules.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { config } from 'dotenv'

config({ path: '../.env' })
config({ path: '../shared/.env' })

import { PrismaClient } from '@prisma/client'

const db = new PrismaClient()

const ruleElements = await db.ruleElement.findMany()

for (const el of ruleElements) {
try {
await db.$queryRaw`select '' ~* ${el.regex}`
} catch (e) {
console.error('----------------------')
console.error(e.message)
console.error('element:', el)
console.error('rule:', await db.rule.findUnique({where: {id: el.ruleId}}))
}
}
2 changes: 1 addition & 1 deletion bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@trpc/server": "9.26.2",
"axios": "^0.27.2",
"discord-hybrid-sharding": "^1.7.2",
"discord.js": "14.1.1",
"discord.js": "14.8.0",
"dokdo": "pikokr/dokdo#v14",
"dotenv": "16.0.1",
"fastify": "4.3.0",
Expand Down
3 changes: 2 additions & 1 deletion bot/src/modules/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
EmbedBuilder,
Interaction,
SelectMenuBuilder,
TextBasedChannel
} from "discord.js"
import { prisma, Rule, RuleType } from "shared"

Expand All @@ -29,7 +30,7 @@ class RuleModule extends Extension {
async tags(i: ChatInputCommandInteraction) {
let channel = i.channel
if (!channel) return
if (channel.isThread()) channel = channel.parent
if (channel.isThread()) channel = channel.parent as TextBasedChannel
if (!channel) return
if (channel.isDMBased()) return i.reply("DM 안 받아요")

Expand Down
1 change: 1 addition & 0 deletions server/src/resolvers/mutation/rule/createRuleElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const createRuleElement: Resolver<
if (info.regex) {
try {
new RegExp(info.regex)
await prisma.$queryRaw`select '' ~* ${info.regex}`
} catch (e) {
throw new ValidationError("Invalid regex")
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/resolvers/mutation/ruleElement/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ export const updateRuleElement: Resolver<
RuleElement,
RuleElement,
{ info: Partial<RuleElementInfo> }
> = (elem, { info }) => {
> = async (elem, { info }) => {
if (info.regex) {
try {
new RegExp(info.regex)
await prisma.$queryRaw`select '' ~* ${info.regex}`
} catch (e) {
throw new ValidationError("Invalid regex")
}
Expand Down
Loading

0 comments on commit 7c4698a

Please sign in to comment.