mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Refactor exception usage
This commit is contained in:
+7
-5
@@ -21,7 +21,7 @@ export const serializedBitmaskRegex = /^(\d*,\d*(,\d*,\d*)*)?$/
|
||||
|
||||
export const validateBitmask = (bitmask: string, maxLength: number) => {
|
||||
if (!serializedBitmaskRegex.test(bitmask)) {
|
||||
throw new Error('bitmask does not match regex')
|
||||
throw new BitmapValidationException('bitmask does not match regex')
|
||||
}
|
||||
|
||||
if (bitmask === '') {
|
||||
@@ -31,22 +31,22 @@ export const validateBitmask = (bitmask: string, maxLength: number) => {
|
||||
const splitpoints = split(bitmask, ',').map((item) => parseInt(item, 10))
|
||||
|
||||
if (splitpoints.findIndex((item) => !Number.isSafeInteger(item)) !== -1) {
|
||||
throw new Error('bitmask contains non-safe integers')
|
||||
throw new BitmapValidationException('bitmask contains non-safe integers')
|
||||
}
|
||||
|
||||
if (splitpoints.findIndex((item) => item < 0) !== -1) {
|
||||
throw new Error('bitmask contains negative integers')
|
||||
throw new BitmapValidationException('bitmask contains negative integers')
|
||||
}
|
||||
|
||||
if (splitpoints.findIndex((item) => item > maxLength) !== -1) {
|
||||
throw new Error('bitmask contains integers bigger than maxSize')
|
||||
throw new BitmapValidationException('bitmask contains integers bigger than maxSize')
|
||||
}
|
||||
|
||||
let previousValue = -1
|
||||
|
||||
splitpoints.forEach((item) => {
|
||||
if (item <= previousValue) {
|
||||
throw new Error('bitmask numbers are not strictly sorted')
|
||||
throw new BitmapValidationException('bitmask numbers are not strictly sorted')
|
||||
}
|
||||
|
||||
previousValue = item
|
||||
@@ -72,3 +72,5 @@ export const validateAndParseBitmask = (bitmask: string, maxLength: number) => {
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export class BitmapValidationException extends Error {}
|
||||
|
||||
@@ -23,7 +23,7 @@ export function getCategoryWithParentCategories (categories: Array<{ categoryId:
|
||||
const startCategory = categoryById.get(startCategoryId)
|
||||
|
||||
if (!startCategory) {
|
||||
throw new Error('start category not found')
|
||||
throw new GetParentCategoriesException('start category not found')
|
||||
}
|
||||
|
||||
const categoryIds = [ startCategoryId ]
|
||||
@@ -38,3 +38,5 @@ export function getCategoryWithParentCategories (categories: Array<{ categoryId:
|
||||
|
||||
return categoryIds
|
||||
}
|
||||
|
||||
export class GetParentCategoriesException extends Error {}
|
||||
|
||||
+14
-4
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
@@ -15,16 +15,26 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export function assertIsHexString (value: string) {
|
||||
export function checkIfHexString (value: string) {
|
||||
if (value.length % 2 !== 0) {
|
||||
throw new Error('expected hex string but has got uneven length')
|
||||
return false
|
||||
}
|
||||
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
const char = value[i]
|
||||
|
||||
if ('0123456789abcdef'.indexOf(char) === -1) {
|
||||
throw new Error('expected hex string but got invalid char')
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
export function assertIsHexString (value: string) {
|
||||
if (!checkIfHexString(value)) {
|
||||
throw new HexStringValidationException('wanted hex string but did not get one')
|
||||
}
|
||||
}
|
||||
|
||||
export class HexStringValidationException extends Error {}
|
||||
|
||||
+3
-15
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
@@ -17,18 +17,6 @@
|
||||
|
||||
import { uniq } from 'lodash'
|
||||
|
||||
export function assertNonEmptyListWithoutDuplicates (list: Array<string>) {
|
||||
if (list.length === 0) {
|
||||
throw new Error('expected not empty list')
|
||||
}
|
||||
|
||||
if (uniq(list).length !== list.length) {
|
||||
throw new Error('expected list without duplicates')
|
||||
}
|
||||
}
|
||||
|
||||
export function assertListWithoutDuplicates (list: Array<string>) {
|
||||
if (uniq(list).length !== list.length) {
|
||||
throw new Error('expected list without duplicates')
|
||||
}
|
||||
export function hasDuplicates (list: Array<string>): boolean {
|
||||
return uniq(list).length !== list.length
|
||||
}
|
||||
|
||||
+3
-2
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
@@ -19,6 +19,7 @@ import { parseOneAddress } from 'email-addresses'
|
||||
import * as Email from 'email-templates'
|
||||
import { join } from 'path'
|
||||
import { config } from '../config'
|
||||
import { IllegalStateException } from '../exception'
|
||||
|
||||
const mailimprint = process.env.MAIL_IMPRINT || 'not defined'
|
||||
const mailServerBlacklist = (process.env.MAIL_SERVER_BLACKLIST || '').split(',').filter((item) => !!item)
|
||||
@@ -129,7 +130,7 @@ export function sanitizeMailAddress (input: string): string | null {
|
||||
const address = (parsed as any).address
|
||||
|
||||
if (typeof address !== 'string') {
|
||||
throw new Error('illegal state')
|
||||
throw new IllegalStateException({ staticMessage: 'mail address is not a string' })
|
||||
}
|
||||
|
||||
return address
|
||||
|
||||
+5
-1
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
import * as TokenGenerator from 'tokgen'
|
||||
import { ValidationException } from '../exception'
|
||||
|
||||
const authTokenGenerator = new TokenGenerator({
|
||||
length: 32,
|
||||
@@ -33,7 +34,10 @@ export const generateIdWithinFamily = () => idWithinFamilyGenerator.generate()
|
||||
export const isIdWithinFamily = (id: string) => id.length === 6 && /^[a-zA-Z0-9]+$/.test(id)
|
||||
export const assertIdWithinFamily = (id: string) => {
|
||||
if (!isIdWithinFamily(id)) {
|
||||
throw new Error('invalid id within family')
|
||||
throw new ValidationException({
|
||||
staticMessage: 'invalid id within family',
|
||||
dynamicMessage: 'invalid id within family: ' + id
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user