Refactor exception usage

This commit is contained in:
Jonas Lochmann
2020-09-28 02:00:00 +02:00
parent d68b425e0e
commit 8d65c5d777
148 changed files with 2061 additions and 769 deletions
@@ -19,6 +19,7 @@ import * as Sequelize from 'sequelize'
import { AddUsedTimeAction } from '../../../../action'
import { MinuteOfDay } from '../../../../util/minuteofday'
import { Cache } from '../cache'
import { MissingCategoryException } from '../exception/missing-item'
export const getRoundedTimestamp = () => {
const now = Date.now()
@@ -29,7 +30,7 @@ export const getRoundedTimestamp = () => {
const dayLengthInMinutes = MinuteOfDay.LENGTH
const dayLengthInMs = dayLengthInMinutes * 1000 * 60
export async function dispatchAddUsedTime ({ deviceId, action, cache }: {
export async function dispatchAddUsedTime ({ action, cache }: {
deviceId: string
action: AddUsedTimeAction
cache: Cache
@@ -50,7 +51,7 @@ export async function dispatchAddUsedTime ({ deviceId, action, cache }: {
})
// verify that the category exists
if (!categoryEntryUnsafe) {
throw new Error('invalid category id')
throw new MissingCategoryException()
}
const categoryEntry = {
@@ -20,6 +20,7 @@ import { AddUsedTimeActionVersion2 } from '../../../../action'
import { EventHandler } from '../../../../monitoring/eventhandler'
import { MinuteOfDay } from '../../../../util/minuteofday'
import { Cache } from '../cache'
import { SourceDeviceNotFoundException } from '../exception/illegal-state'
import { getRoundedTimestamp as getRoundedTimestampForUsedTime } from './addusedtime'
export const getRoundedTimestampForSessionDuration = () => {
@@ -44,7 +45,7 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache, ev
})
if (!deviceEntryUnsafe) {
throw new Error('source device not found')
throw new SourceDeviceNotFoundException()
}
const deviceEntry = {
@@ -56,9 +57,7 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache, ev
let addUsedTimeForADifferentUserThanTheCurrentUserOfTheDevice = false
for (let i = 0; i < action.items.length; i++) {
const item = action.items[i]
for (const item of action.items) {
const categoryEntryUnsafe = await cache.database.category.findOne({
where: {
familyId: cache.familyId,
@@ -73,9 +72,10 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache, ev
// verify that the category exists
if (!categoryEntryUnsafe) {
eventHandler.countEvent('add used time category to add time for not found')
cache.requireFullSync()
return
continue
}
const categoryEntry = {
@@ -29,6 +29,7 @@ import {
} from '../../../../action'
import { EventHandler } from '../../../../monitoring/eventhandler'
import { Cache } from '../cache'
import { ActionObjectTypeNotHandledException } from '../exception/illegal-state'
import { dispatchAddInstalledApps } from './addinstalledapps'
import { dispatchAddUsedTime } from './addusedtime'
import { dispatchAddUsedTimeVersion2 } from './addusedtime2'
@@ -64,6 +65,6 @@ export const dispatchAppLogicAction = async ({ action, deviceId, cache, eventHan
} else if (action instanceof TriedDisablingDeviceAdminAction) {
await dispatchTriedDisablingDeviceAdmin({ deviceId, action, cache })
} else {
throw new Error('unsupported action type')
throw new ActionObjectTypeNotHandledException()
}
}
@@ -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
@@ -18,14 +18,16 @@
import { SetDeviceUserAction, SignOutAtDeviceAction } from '../../../../action'
import { Cache } from '../cache'
import { dispatchSetDeviceUser } from '../dispatch-parent-action/setdeviceuser'
import { IllegalStateException, SourceDeviceNotFoundException } from '../exception/illegal-state'
import { PremiumVersionMissingException } from '../exception/premium'
export async function dispatchSignOutAtDevice ({ deviceId, action, cache }: {
export async function dispatchSignOutAtDevice ({ deviceId, cache }: {
deviceId: string
action: SignOutAtDeviceAction
cache: Cache
}) {
if (!cache.hasFullVersion) {
throw new Error('action requires full version')
throw new PremiumVersionMissingException()
}
const deviceEntry = await cache.database.device.findOne({
@@ -37,11 +39,13 @@ export async function dispatchSignOutAtDevice ({ deviceId, action, cache }: {
})
if (!deviceEntry) {
throw new Error('illegal state: missing device which dispatched the action')
throw new SourceDeviceNotFoundException()
}
if (deviceEntry.defaultUserId === '') {
throw new Error('no default user available')
throw new IllegalStateException({
staticMessage: 'tried to switch to the default user where it does not exist'
})
}
if (deviceEntry.currentUserId !== deviceEntry.defaultUserId) {
@@ -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,8 +19,9 @@ import { TriedDisablingDeviceAdminAction } from '../../../../action'
import { hasDeviceManipulation } from '../../../../database/device'
import { sendManipulationWarnings } from '../../../warningmail/manipulation'
import { Cache } from '../cache'
import { SourceDeviceNotFoundException } from '../exception/illegal-state'
export async function dispatchTriedDisablingDeviceAdmin ({ deviceId, action, cache }: {
export async function dispatchTriedDisablingDeviceAdmin ({ deviceId, cache }: {
deviceId: string
action: TriedDisablingDeviceAdminAction
cache: Cache
@@ -34,7 +35,7 @@ export async function dispatchTriedDisablingDeviceAdmin ({ deviceId, action, cac
})
if (deviceEntry === null) {
throw new Error('illegal state: missing device which dispatched the action')
throw new SourceDeviceNotFoundException()
}
const hadManipulationBefore = hasDeviceManipulation(deviceEntry)
@@ -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
@@ -29,9 +29,7 @@ export async function dispatchUpdateAppActivities ({ deviceId, action, cache }:
if (action.updatedOrAdded.length > 0) {
const chuncks = chunk(action.updatedOrAdded, 500)
for (let i = 0; i < chuncks.length; i++) {
const items = chuncks[i]
for (const items of chuncks) {
await cache.database.appActivity.destroy({
where: {
familyId: cache.familyId,
@@ -62,9 +60,7 @@ export async function dispatchUpdateAppActivities ({ deviceId, action, cache }:
if (action.removed.length > 0) {
const chunks = chunk(action.removed, 500)
for (let i = 0; i < chunks.length; i++) {
const items = chunks[i]
for (const items of chunks) {
await cache.database.appActivity.destroy({
where: {
familyId: cache.familyId,
@@ -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
@@ -23,6 +23,7 @@ import { runtimePermissionStatusValues } from '../../../../model/runtimepermissi
import { enumMax } from '../../../../util/enum'
import { sendManipulationWarnings } from '../../../warningmail/manipulation'
import { Cache } from '../cache'
import { SourceDeviceNotFoundException } from '../exception/illegal-state'
export async function dispatchUpdateDeviceStatus ({ deviceId, action, cache }: {
deviceId: string
@@ -38,7 +39,7 @@ export async function dispatchUpdateDeviceStatus ({ deviceId, action, cache }: {
})
if (!deviceEntry) {
throw new Error('device not found')
throw new SourceDeviceNotFoundException()
}
const hadManipulationBefore = hasDeviceManipulation(deviceEntry)