mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 22:46:20 -05:00
Splitting errors into their modules. Removing API_ERRORS dict in favor of storing error data on the class.
This commit is contained in:
parent
b3068885b2
commit
fdaaa510c4
33 changed files with 337 additions and 449 deletions
|
|
@ -36,8 +36,9 @@ async function apiCall(url, method, body, ms = 60_000) {
|
|||
}
|
||||
// Request encountered an error.
|
||||
let copy = response.clone();
|
||||
let code = (await copy.json())['code'];
|
||||
if (response.status === 403 && code === 17) {
|
||||
const content = await copy.json();
|
||||
const code = content['code'];
|
||||
if (response.status === 403 && code === 'WROL_MODE_ENABLED') {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'WROL Mode is enabled!',
|
||||
|
|
@ -234,7 +235,7 @@ export async function downloadChannel(channelId) {
|
|||
let response = await apiPost(url);
|
||||
if (response.status === 400) {
|
||||
const json = await response.json();
|
||||
if (json['code'] === 30) {
|
||||
if (json['code'] === 'INVALID_DOWNLOAD') {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Cannot Download!',
|
||||
|
|
@ -585,7 +586,7 @@ export async function deleteFile(paths) {
|
|||
toast({
|
||||
type: 'error',
|
||||
title: 'Delete error',
|
||||
description: content['message'],
|
||||
description: content['message'] || content['summary'],
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
|
|
@ -614,12 +615,13 @@ export async function setHotspot(on) {
|
|||
if (response.status === 204) {
|
||||
return null;
|
||||
} else {
|
||||
let code = (await response.json())['code'];
|
||||
if (code === 34) {
|
||||
const content = await response.json();
|
||||
const code = content['code'];
|
||||
if (code === 'NATIVE_ONLY') {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Unsupported!',
|
||||
description: 'Hotspot is only supported on a Raspberry Pi!',
|
||||
description: 'Hotspot is only supported outside Docker!',
|
||||
time: 5000,
|
||||
});
|
||||
} else {
|
||||
|
|
@ -645,8 +647,9 @@ export async function setThrottle(on) {
|
|||
if (response.status === 204) {
|
||||
return null;
|
||||
} else {
|
||||
let code = (await response.json())['code'];
|
||||
if (code === 34) {
|
||||
const content = await response.json();
|
||||
const code = content['code'];
|
||||
if (code === 'NATIVE_ONLY') {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Unsupported!',
|
||||
|
|
@ -810,9 +813,9 @@ export async function deleteTag(id, name) {
|
|||
let response = await apiDelete(uri);
|
||||
if (response.status === 400) {
|
||||
const content = await response.json();
|
||||
if (content['code'] === 38) {
|
||||
if (content['code'] === 'USED_TAG') {
|
||||
toast({
|
||||
type: 'error', title: 'Error!', description: content['message'], time: 5000,
|
||||
type: 'error', title: 'Error!', description: content['error'] || content['summary'], time: 5000,
|
||||
})
|
||||
}
|
||||
} else if (response.status !== 204) {
|
||||
|
|
|
|||
|
|
@ -187,19 +187,19 @@ function ChannelPage({create, header}) {
|
|||
// Some error occurred.
|
||||
let error = await response.json();
|
||||
let cause = error.cause;
|
||||
if (error.code === 3 || (cause && cause.code === 3)) {
|
||||
if (error.code === 'UNKNOWN_DIRECTORY' || (cause && cause.code === 'UNKNOWN_DIRECTORY')) {
|
||||
setErrorMessage(
|
||||
'Invalid directory',
|
||||
'This directory does not exist.',
|
||||
'directory',
|
||||
);
|
||||
} else if (cause && cause.code === 7) {
|
||||
} else if (cause && cause.code === 'CHANNEL_DIRECTORY_CONFLICT') {
|
||||
setErrorMessage(
|
||||
'Invalid directory',
|
||||
'This directory is already used by another channel',
|
||||
'directory',
|
||||
);
|
||||
} else if (cause && cause.code === 5) {
|
||||
} else if (cause && cause.code === 'CHANNEL_NAME_CONFLICT') {
|
||||
setErrorMessage(
|
||||
'Invalid name',
|
||||
'This channel name is already taken',
|
||||
|
|
|
|||
|
|
@ -891,7 +891,7 @@ export const useUploadFile = () => {
|
|||
} else if (xhr.status === 400) {
|
||||
handleProgress(file.name, totalChunks, totalChunks, 'conflicting', file.type);
|
||||
const data = JSON.parse(xhr.responseText);
|
||||
if (data['code'] === 41) {
|
||||
if (data['code'] === 'FILE_UPLOAD_FAILED') {
|
||||
console.error('File already exists. Giving up.');
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@ from wrolpi.common import logger, register_modeler, register_refresh_cleanup, li
|
|||
slow_logger
|
||||
from wrolpi.db import optional_session, get_db_session
|
||||
from wrolpi.downloader import Downloader, Download, DownloadResult
|
||||
from wrolpi.errors import UnrecoverableDownloadError, InvalidArchive
|
||||
from wrolpi.errors import UnrecoverableDownloadError
|
||||
from .errors import InvalidArchive
|
||||
from wrolpi.files.models import FileGroup
|
||||
from wrolpi.vars import PYTEST
|
||||
from . import lib
|
||||
|
|
|
|||
9
modules/archive/errors.py
Normal file
9
modules/archive/errors.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
from http import HTTPStatus
|
||||
|
||||
from wrolpi.errors import APIError
|
||||
|
||||
|
||||
class InvalidArchive(APIError):
|
||||
code = 'INVALID_ARCHIVE'
|
||||
summary = 'The archive is invalid. See server logs.'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
|
@ -11,7 +11,7 @@ from sqlalchemy.orm.collections import InstrumentedList
|
|||
|
||||
from wrolpi.common import ModelHelper, Base, logger
|
||||
from wrolpi.dates import TZDateTime
|
||||
from wrolpi.errors import InvalidArchive
|
||||
from modules.archive import InvalidArchive
|
||||
from wrolpi.files.models import FileGroup
|
||||
from wrolpi.media_path import MediaPathType
|
||||
from wrolpi.tags import Tag, TagFile
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ from pint import Quantity
|
|||
from wrolpi import before_startup
|
||||
from wrolpi.common import logger, Base, ConfigFile
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.errors import NoInventories, InventoriesVersionMismatch
|
||||
from .errors import NoInventories, InventoriesVersionMismatch
|
||||
from .inventory import unit_registry, get_items, get_inventories, increment_inventories_version, \
|
||||
get_inventories_version
|
||||
from .models import Inventory, Item
|
||||
|
|
|
|||
15
modules/inventory/errors.py
Normal file
15
modules/inventory/errors.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
from http import HTTPStatus
|
||||
|
||||
from wrolpi.errors import APIError
|
||||
|
||||
|
||||
class NoInventories(APIError):
|
||||
code = 'NO_INVENTORIES'
|
||||
summary = 'No Inventories'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class InventoriesVersionMismatch(APIError):
|
||||
code = 'INVENTORIES_VERSION_MISMATCH'
|
||||
summary = 'Inventories version in the DB does not match the inventories config'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
|
@ -10,7 +10,7 @@ from pint import Quantity
|
|||
|
||||
from wrolpi.common import Base
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.errors import NoInventories, InventoriesVersionMismatch
|
||||
from ..errors import NoInventories, InventoriesVersionMismatch
|
||||
from wrolpi.test.common import PytestCase
|
||||
from .. import init
|
||||
from ..common import sum_by_key, get_inventory_by_category, get_inventory_by_subcategory, get_inventory_by_name, \
|
||||
|
|
|
|||
|
|
@ -135,4 +135,4 @@ def test_empty_import(test_client, test_session, test_directory):
|
|||
body = {'files': []}
|
||||
request, response = test_client.post('/api/map/import', content=json.dumps(body))
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert 'validate' in response.json['error']
|
||||
assert 'validate' in response.json['summary']
|
||||
|
|
|
|||
21
modules/otp/errors.py
Normal file
21
modules/otp/errors.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
from http import HTTPStatus
|
||||
|
||||
from wrolpi.errors import APIError
|
||||
|
||||
|
||||
class InvalidOTP(APIError):
|
||||
code = 'INVALID_OTP'
|
||||
summary = 'OTP has invalid characters'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class InvalidPlaintext(APIError):
|
||||
code = 'INVALID_PLAINTEXT'
|
||||
summary = 'Plaintext has invalid characters'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class InvalidCiphertext(APIError):
|
||||
code = 'INVALID_CIPHERTEXT'
|
||||
summary = 'Ciphertext has invalid characters'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
|
@ -3,7 +3,7 @@ from functools import partial
|
|||
from typing import Dict
|
||||
|
||||
from wrolpi.common import chunks, remove_whitespace, logger
|
||||
from wrolpi.errors import InvalidOTP, InvalidPlaintext, InvalidCiphertext
|
||||
from modules.otp.errors import InvalidOTP, InvalidPlaintext, InvalidCiphertext
|
||||
|
||||
# These are the only characters we support.
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ from sanic_ext.extensions.openapi import openapi
|
|||
|
||||
from wrolpi.common import logger, wrol_mode_check, make_media_directory, \
|
||||
get_media_directory, run_after
|
||||
from wrolpi.errors import UnknownChannel
|
||||
from ..errors import UnknownChannel
|
||||
from wrolpi.root_api import json_response
|
||||
from wrolpi.schema import JSONErrorResponse
|
||||
from wrolpi.vars import PYTEST
|
||||
|
|
|
|||
|
|
@ -8,7 +8,8 @@ from sqlalchemy.orm.exc import NoResultFound
|
|||
from wrolpi.common import run_after, logger, \
|
||||
get_media_directory
|
||||
from wrolpi.db import get_db_curs, optional_session
|
||||
from wrolpi.errors import UnknownChannel, UnknownDirectory, APIError, ValidationError, InvalidDownload
|
||||
from wrolpi.errors import UnknownDirectory, APIError, ValidationError, InvalidDownload
|
||||
from ..errors import UnknownChannel
|
||||
from .. import schema
|
||||
from ..common import check_for_channel_conflicts
|
||||
from ..lib import save_channels_config
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ def test_get_video(test_client, test_session, simple_channel, video_factory):
|
|||
# Test that a 404 is returned when no video exists
|
||||
_, response = test_client.get('/api/videos/video/10')
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND, response.json
|
||||
assert response.json == {'code': 1, 'api_error': 'The video could not be found.', 'message': ''}
|
||||
assert response.json == {'code': 'UNKNOWN_VIDEO', 'summary': 'The video could not be found.', 'error': ''}
|
||||
|
||||
# Get the video info we inserted
|
||||
_, response = test_client.get('/api/videos/video/1')
|
||||
|
|
@ -203,8 +203,12 @@ def test_channel_conflicts(test_client, test_session, test_directory):
|
|||
)
|
||||
request, response = _post_channel(new_channel)
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert response.json == {'error': 'Could not validate the contents of the request', 'code': 10,
|
||||
'cause': {'error': 'The channel name is already taken.', 'code': 5}}
|
||||
assert response.json == {'cause': {'code': 'CHANNEL_NAME_CONFLICT',
|
||||
'error': '',
|
||||
'summary': 'The channel name is already taken.'},
|
||||
'code': 'VALIDATION_ERROR',
|
||||
'error': '',
|
||||
'summary': 'Could not validate the contents of the request'}
|
||||
|
||||
# Directory was already used
|
||||
new_channel = dict(
|
||||
|
|
@ -213,8 +217,12 @@ def test_channel_conflicts(test_client, test_session, test_directory):
|
|||
)
|
||||
request, response = _post_channel(new_channel)
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert response.json == {'error': 'Could not validate the contents of the request', 'code': 10,
|
||||
'cause': {'code': 7, 'error': 'The directory is already used by another channel.'}}
|
||||
assert response.json == {'cause': {'code': 'CHANNEL_DIRECTORY_CONFLICT',
|
||||
'error': '',
|
||||
'summary': 'The directory is already used by another channel.'},
|
||||
'code': 'VALIDATION_ERROR',
|
||||
'error': '',
|
||||
'summary': 'Could not validate the contents of the request'}
|
||||
|
||||
# URL is already used
|
||||
channel_directory3 = tempfile.TemporaryDirectory(dir=test_directory).name
|
||||
|
|
@ -226,8 +234,12 @@ def test_channel_conflicts(test_client, test_session, test_directory):
|
|||
)
|
||||
request, response = _post_channel(new_channel)
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert response.json == {'error': 'Could not validate the contents of the request', 'code': 10,
|
||||
'cause': {'code': 6, 'error': 'The URL is already used by another channel.'}}
|
||||
assert response.json == {'cause': {'code': 'CHANNEL_URL_CONFLICT',
|
||||
'error': '',
|
||||
'summary': 'The URL is already used by another channel.'},
|
||||
'code': 'VALIDATION_ERROR',
|
||||
'error': '',
|
||||
'summary': 'Could not validate the contents of the request'}
|
||||
|
||||
|
||||
def test_channel_empty_url_doesnt_conflict(test_client, test_session, test_directory):
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ from modules.videos import schema
|
|||
from modules.videos.channel import lib
|
||||
from modules.videos.lib import save_channels_config, import_channels_config
|
||||
from modules.videos.models import Channel
|
||||
from wrolpi.errors import UnknownChannel
|
||||
from modules.videos.errors import UnknownChannel
|
||||
|
||||
|
||||
@pytest.mark.parametrize('params', [
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ async def test_nested_channel_directories(test_session, test_async_client, test_
|
|||
request, response = await test_async_client.post('/api/videos/channels', content=json.dumps(content))
|
||||
assert response.status == HTTPStatus.BAD_REQUEST
|
||||
assert response.json['cause'] and \
|
||||
response.json['cause']['error'] == 'The directory is already used by another channel.'
|
||||
response.json['cause']['summary'] == 'The directory is already used by another channel.'
|
||||
|
||||
# Channel 3 cannot be above Channel 1's directory.
|
||||
content = dict(
|
||||
|
|
@ -65,4 +65,4 @@ async def test_nested_channel_directories(test_session, test_async_client, test_
|
|||
request, response = await test_async_client.post('/api/videos/channels', content=json.dumps(content))
|
||||
assert response.status == HTTPStatus.BAD_REQUEST
|
||||
assert response.json['cause'] and \
|
||||
response.json['cause']['error'] == 'The directory is already used by another channel.'
|
||||
response.json['cause']['summary'] == 'The directory is already used by another channel.'
|
||||
|
|
|
|||
|
|
@ -17,8 +17,7 @@ from wrolpi.captions import FFMPEG_BIN
|
|||
from wrolpi.cmd import FFPROBE_BIN
|
||||
from wrolpi.common import logger, get_media_directory
|
||||
from wrolpi.db import get_db_session, get_db_curs
|
||||
from wrolpi.errors import ChannelNameConflict, ChannelURLConflict, \
|
||||
ChannelDirectoryConflict, ChannelSourceIdConflict
|
||||
from .errors import ChannelNameConflict, ChannelURLConflict, ChannelDirectoryConflict, ChannelSourceIdConflict
|
||||
from wrolpi.vars import DEFAULT_FILE_PERMISSIONS
|
||||
from .models import Channel
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,8 @@ from wrolpi.dates import now
|
|||
from wrolpi.db import get_db_session
|
||||
from wrolpi.db import optional_session
|
||||
from wrolpi.downloader import Downloader, Download, DownloadResult
|
||||
from wrolpi.errors import UnknownChannel, ChannelURLEmpty, UnrecoverableDownloadError
|
||||
from wrolpi.errors import UnrecoverableDownloadError
|
||||
from .errors import UnknownChannel, ChannelURLEmpty
|
||||
from wrolpi.files.lib import glob_shared_stem
|
||||
from wrolpi.vars import PYTEST
|
||||
from .channel.lib import create_channel, get_channel
|
||||
|
|
|
|||
45
modules/videos/errors.py
Normal file
45
modules/videos/errors.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
from http import HTTPStatus
|
||||
|
||||
from wrolpi.errors import APIError
|
||||
|
||||
|
||||
class UnknownVideo(APIError):
|
||||
code = 'UNKNOWN_VIDEO'
|
||||
summary = 'The video could not be found.'
|
||||
status = HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
class UnknownChannel(APIError):
|
||||
code = 'UNKNOWN_CHANNEL'
|
||||
summary = 'The channel could not be found.'
|
||||
status = HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
class ChannelNameConflict(APIError):
|
||||
code = 'CHANNEL_NAME_CONFLICT'
|
||||
summary = 'The channel name is already taken.'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class ChannelURLConflict(APIError):
|
||||
code = 'CHANNEL_URL_CONFLICT'
|
||||
summary = 'The URL is already used by another channel.'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class ChannelDirectoryConflict(APIError):
|
||||
code = 'CHANNEL_DIRECTORY_CONFLICT'
|
||||
summary = 'The directory is already used by another channel.'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class ChannelSourceIdConflict(APIError):
|
||||
code = 'CHANNEL_SOURCE_ID_CONFLICT'
|
||||
summary = 'Search is empty, search_str must have content.'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class ChannelURLEmpty(APIError):
|
||||
code = 'CHANNEL_URL_EMPTY'
|
||||
summary = 'This channel has no URL'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
|
@ -13,7 +13,7 @@ from wrolpi.common import Base, ModelHelper, logger, get_media_directory, backgr
|
|||
from wrolpi.dates import now, TZDateTime
|
||||
from wrolpi.db import get_db_curs, get_db_session, optional_session
|
||||
from wrolpi.downloader import Download, download_manager
|
||||
from wrolpi.errors import UnknownVideo
|
||||
from modules.videos.errors import UnknownVideo
|
||||
from wrolpi.files.lib import refresh_files, split_path_stem_and_suffix
|
||||
from wrolpi.files.models import FileGroup
|
||||
from wrolpi.media_path import MediaPathType
|
||||
|
|
|
|||
|
|
@ -150,7 +150,7 @@ def test_api_download_channel(test_session, test_client, simple_channel):
|
|||
# simple_channel does not have a download record.
|
||||
request, response = test_client.post(f'/api/videos/channels/download/{simple_channel.id}')
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST, response.json
|
||||
assert 'not have a download' in response.json['message']
|
||||
assert 'not have a download' in response.json['error']
|
||||
|
||||
# Add a download frequency to the channel, this should also create a download.
|
||||
simple_channel.update(dict(download_frequency=DownloadFrequency.daily))
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from sqlalchemy.orm.exc import NoResultFound
|
|||
from modules.videos.models import Video
|
||||
from wrolpi.common import run_after, logger
|
||||
from wrolpi.db import get_db_session, optional_session
|
||||
from wrolpi.errors import UnknownVideo
|
||||
from ..errors import UnknownVideo
|
||||
from wrolpi.files.lib import handle_file_group_search_results, tag_names_to_file_group_sub_select
|
||||
from ..lib import save_channels_config
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import pytest
|
|||
from modules.videos.models import Video
|
||||
from modules.videos.video.lib import get_video_for_app
|
||||
from wrolpi.dates import now
|
||||
from wrolpi.errors import API_ERRORS, WROLModeEnabled
|
||||
from wrolpi.errors import WROLModeEnabled
|
||||
|
||||
|
||||
def test_get_video_prev_next(test_session, channel_factory, video_factory):
|
||||
|
|
@ -133,26 +133,26 @@ async def test_wrol_mode(test_async_client, test_directory, simple_channel, simp
|
|||
# Can't create, update, or delete a channel.
|
||||
_, resp = await test_async_client.post('/api/videos/channels', content=channel)
|
||||
assert resp.status_code == HTTPStatus.FORBIDDEN
|
||||
assert resp.json['code'] == API_ERRORS[WROLModeEnabled]['code']
|
||||
assert resp.json['code'] == 'WROL_MODE_ENABLED'
|
||||
_, resp = await test_async_client.put(f'/api/videos/channels/{simple_channel.id}', content=channel)
|
||||
assert resp.status_code == HTTPStatus.FORBIDDEN
|
||||
assert resp.json['code'] == API_ERRORS[WROLModeEnabled]['code']
|
||||
assert resp.json['code'] == 'WROL_MODE_ENABLED'
|
||||
_, resp = await test_async_client.delete(f'/api/videos/channels/{simple_channel.id}')
|
||||
assert resp.status_code == HTTPStatus.FORBIDDEN
|
||||
assert resp.json['code'] == API_ERRORS[WROLModeEnabled]['code']
|
||||
assert resp.json['code'] == 'WROL_MODE_ENABLED'
|
||||
|
||||
# Can't delete a video
|
||||
_, resp = await test_async_client.delete(f'/api/videos/video/{simple_video.id}')
|
||||
assert resp.status_code == HTTPStatus.FORBIDDEN
|
||||
assert resp.json['code'] == API_ERRORS[WROLModeEnabled]['code']
|
||||
assert resp.json['code'] == 'WROL_MODE_ENABLED'
|
||||
|
||||
# Can't refresh or download
|
||||
_, resp = await test_async_client.post('/api/files/refresh')
|
||||
assert resp.status_code == HTTPStatus.FORBIDDEN
|
||||
assert resp.json['code'] == API_ERRORS[WROLModeEnabled]['code']
|
||||
assert resp.json['code'] == 'WROL_MODE_ENABLED'
|
||||
_, resp = await test_async_client.post(f'/api/videos/channels/download/{simple_channel.id}')
|
||||
assert resp.status_code == HTTPStatus.FORBIDDEN
|
||||
assert resp.json['code'] == API_ERRORS[WROLModeEnabled]['code']
|
||||
assert resp.json['code'] == 'WROL_MODE_ENABLED'
|
||||
|
||||
# THE REST OF THESE METHODS ARE ALLOWED
|
||||
content = dict(video_id=simple_video.id, tag_name=tag.name)
|
||||
|
|
|
|||
27
modules/zim/errors.py
Normal file
27
modules/zim/errors.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from http import HTTPStatus
|
||||
|
||||
from wrolpi.errors import APIError
|
||||
|
||||
|
||||
class UnknownZim(APIError):
|
||||
code = 'UNKNOWN_ZIM'
|
||||
summary = 'Failed to find the Zim'
|
||||
status = HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
class UnknownZimEntry(APIError):
|
||||
code = 'UNKNOWN_ZIM_ENTRY'
|
||||
summary = 'Failed to find the Zim entry at that path'
|
||||
status = HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
class UnknownZimTagEntry(APIError):
|
||||
code = 'UNKNOWN_ZIM_TAG_ENTRY'
|
||||
summary = 'Failed to find the TagZimEntry'
|
||||
status = HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
class UnknownZimSubscription(APIError):
|
||||
code = 'UNKNOWN_ZIM_SUBSCRIPTION'
|
||||
summary = 'Failed to find the ZimSubscription'
|
||||
status = HTTPStatus.NOT_FOUND
|
||||
|
|
@ -17,7 +17,7 @@ from wrolpi.common import register_modeler, logger, extract_html_text, extract_h
|
|||
register_refresh_cleanup, background_task
|
||||
from wrolpi.db import get_db_session, optional_session, get_db_curs
|
||||
from wrolpi.downloader import DownloadFrequency
|
||||
from wrolpi.errors import UnknownZim, UnknownZimSubscription
|
||||
from modules.zim.errors import UnknownZim, UnknownZimSubscription
|
||||
from wrolpi.files.lib import refresh_files
|
||||
from wrolpi.files.models import FileGroup
|
||||
from wrolpi.vars import PYTEST, DOCKERIZED
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ from wrolpi.common import Base, extract_html_text, logger, get_relative_to_media
|
|||
from wrolpi.dates import TZDateTime
|
||||
from wrolpi.db import optional_session, get_db_curs
|
||||
from wrolpi.downloader import Download, download_manager
|
||||
from wrolpi.errors import UnknownZimEntry, UnknownZimTagEntry
|
||||
from modules.zim.errors import UnknownZimEntry, UnknownZimTagEntry
|
||||
from wrolpi.files.models import FileGroup
|
||||
from wrolpi.media_path import MediaPathType
|
||||
from wrolpi.tags import Tag, tag_names_to_zim_sub_select
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ from modules.zim.models import TagZimEntry, ZimSubscription
|
|||
from wrolpi import tags, flags
|
||||
from wrolpi.common import DownloadFileInfo
|
||||
from wrolpi.downloader import Download, import_downloads_config, save_downloads_config
|
||||
from wrolpi.errors import UnknownZim
|
||||
from modules.zim.errors import UnknownZim
|
||||
from wrolpi.files import lib as files_lib
|
||||
from wrolpi.files.models import FileGroup
|
||||
|
||||
|
|
|
|||
|
|
@ -62,17 +62,17 @@ class Download(ModelHelper, Base): # noqa
|
|||
url = Column(String, nullable=False)
|
||||
|
||||
attempts = Column(Integer, default=0)
|
||||
downloader = Column(Text)
|
||||
error = Column(Text)
|
||||
frequency = Column(Integer)
|
||||
info_json = Column(JSONB)
|
||||
downloader = Column(Text) # 'videos', 'archive', 'kiwix_zim', etc.
|
||||
sub_downloader = Column(Text) # The downloader any returned downloads should be sent.
|
||||
error = Column(Text) # traceback from an error during downloading.
|
||||
frequency = Column(Integer) # seconds between re-downloading.
|
||||
info_json = Column(JSONB) # information retrieve WHILE downloading (from yt-dlp)
|
||||
last_successful_download = Column(TZDateTime)
|
||||
location = Column(Text)
|
||||
location = Column(Text) # Relative App URL where the item is downloaded
|
||||
next_download = Column(TZDateTime)
|
||||
settings = Column(JSONB)
|
||||
status = Column(String, default='new')
|
||||
sub_downloader = Column(Text)
|
||||
_manager = None
|
||||
settings = Column(JSONB) # information about how the download should happen (destination, etc.)
|
||||
status = Column(String, default='new') # 'new', 'pending', 'complete', 'failed', 'deferred'
|
||||
_manager: 'DownloadManager' = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
|
|
|||
429
wrolpi/errors.py
429
wrolpi/errors.py
|
|
@ -1,428 +1,121 @@
|
|||
from http import HTTPStatus
|
||||
|
||||
|
||||
# TODO remove service-specific errors
|
||||
|
||||
|
||||
class APIError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class UnknownVideo(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class UnknownChannel(APIError):
|
||||
pass
|
||||
code = 'APIError'
|
||||
summary = 'No summary defined'
|
||||
status = HTTPStatus.INTERNAL_SERVER_ERROR
|
||||
|
||||
|
||||
class UnknownDirectory(APIError):
|
||||
pass
|
||||
code = 'UNKNOWN_DIRECTORY'
|
||||
summary = 'The directory does not exist.'
|
||||
status = HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
class UnknownFile(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class ChannelNameConflict(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class ChannelURLConflict(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class ChannelDirectoryConflict(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class ChannelSourceIdConflict(APIError):
|
||||
pass
|
||||
code = 'UNKNOWN_FILE'
|
||||
summary = 'The file does not exist.'
|
||||
status = HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
class SearchEmpty(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class UnknownCaptionFile(APIError):
|
||||
pass
|
||||
code = 'SEARCH_EMPTY'
|
||||
summary = 'Search is empty, search_str must have content.'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class ValidationError(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class ChannelLinkConflict(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class BadFieldType(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class MissingRequiredField(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class ExcessJSONFields(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class NoBodyContents(APIError):
|
||||
pass
|
||||
code = 'VALIDATION_ERROR'
|
||||
summary = 'Could not validate the contents of the request'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class InvalidOrderBy(ValidationError):
|
||||
pass
|
||||
code = 'INVALID_ORDER_BY'
|
||||
summary = 'Invalid order_by'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class WROLModeEnabled(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class ChannelURLEmpty(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidOTP(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidPlaintext(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidCiphertext(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class NoFrequency(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class NoInventories(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class InventoriesVersionMismatch(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidTimezone(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidDomain(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class UnknownURL(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class PendingArchive(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidArchive(APIError):
|
||||
pass
|
||||
code = 'WROL_MODE_ENABLED'
|
||||
summary = 'This method is disabled while WROL Mode is enabled.'
|
||||
status = HTTPStatus.FORBIDDEN
|
||||
|
||||
|
||||
class InvalidDownload(APIError):
|
||||
pass
|
||||
code = 'INVALID_DOWNLOAD'
|
||||
summary = 'The URL cannot be downloaded.'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class UnrecoverableDownloadError(APIError):
|
||||
pass
|
||||
code = 'UNRECOVERABLE_DOWNLOAD_ERROR'
|
||||
summary = 'Download experienced an error which cannot be recovered. Download will be deleted.'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class InvalidFile(APIError):
|
||||
pass
|
||||
code = 'INVALID_FILE'
|
||||
summary = 'File does not exist or is a directory'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class NativeOnly(APIError):
|
||||
pass
|
||||
code = 'NATIVE_ONLY'
|
||||
summary = 'This functionality is only supported outside a docker container'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class HotspotError(APIError):
|
||||
pass
|
||||
code = 'HOTSPOT_ERROR'
|
||||
summary = 'Updating/accessing Hotspot encountered an error'
|
||||
status = HTTPStatus.INTERNAL_SERVER_ERROR
|
||||
|
||||
|
||||
class UnknownArchive(APIError):
|
||||
pass
|
||||
code = 'UNKNOWN_ARCHIVE'
|
||||
summary = 'Unable to find the archive'
|
||||
status = HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
class UnknownTag(APIError):
|
||||
pass
|
||||
code = 'UNKNOWN_TAG'
|
||||
summary = 'Unable to find the tag'
|
||||
status = HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
class UsedTag(APIError):
|
||||
pass
|
||||
code = 'USED_TAG'
|
||||
summary = 'Tag is being used'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class InvalidTag(APIError):
|
||||
pass
|
||||
code = 'INVALID_TAG'
|
||||
summary = 'Tag is not allowed'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class FileGroupIsTagged(APIError):
|
||||
pass
|
||||
code = 'FILE_GROUP_IS_TAGGED'
|
||||
summary = 'File group is tagged'
|
||||
status = HTTPStatus.CONFLICT
|
||||
|
||||
|
||||
class FileUploadFailed(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class UnknownZim(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class UnknownZimEntry(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class UnknownZimTagEntry(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class UnknownZimSubscription(APIError):
|
||||
pass
|
||||
code = 'FILE_UPLOAD_FAILED'
|
||||
summary = 'Failed to upload file'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
class FileConflict(APIError):
|
||||
pass
|
||||
code = 'FILE_CONFLICT'
|
||||
summary = 'File/directory already exists'
|
||||
status = HTTPStatus.CONFLICT
|
||||
|
||||
|
||||
error_codes = iter(range(1, 1000))
|
||||
|
||||
API_ERRORS = {
|
||||
UnknownVideo: {
|
||||
'code': next(error_codes),
|
||||
'message': 'The video could not be found.',
|
||||
'status': HTTPStatus.NOT_FOUND,
|
||||
},
|
||||
UnknownChannel: {
|
||||
'code': next(error_codes),
|
||||
'message': 'The channel could not be found.',
|
||||
'status': HTTPStatus.NOT_FOUND,
|
||||
},
|
||||
UnknownDirectory: {
|
||||
'code': next(error_codes),
|
||||
'message': 'The directory does not exist.',
|
||||
'status': HTTPStatus.NOT_FOUND,
|
||||
},
|
||||
UnknownFile: {
|
||||
'code': next(error_codes),
|
||||
'message': 'The file does not exist.',
|
||||
'status': HTTPStatus.NOT_FOUND,
|
||||
},
|
||||
ChannelNameConflict: {
|
||||
'code': next(error_codes),
|
||||
'message': 'The channel name is already taken.',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
ChannelURLConflict: {
|
||||
'code': next(error_codes),
|
||||
'message': 'The URL is already used by another channel.',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
ChannelDirectoryConflict: {
|
||||
'code': next(error_codes),
|
||||
'message': 'The directory is already used by another channel.',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
SearchEmpty: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Search is empty, search_str must have content.',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
UnknownCaptionFile: {
|
||||
'code': next(error_codes),
|
||||
'message': 'The caption file could not be found',
|
||||
'status': HTTPStatus.NOT_FOUND,
|
||||
},
|
||||
ValidationError: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Could not validate the contents of the request',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
ChannelLinkConflict: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Channel link already used by another channel',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
BadFieldType: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Field could not be converted to the required type',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
MissingRequiredField: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Missing required field',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
ExcessJSONFields: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Extra fields in request',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
NoBodyContents: {
|
||||
'code': next(error_codes),
|
||||
'message': 'No content in the body of the request',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
InvalidOrderBy: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Invalid order_by',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
WROLModeEnabled: {
|
||||
'code': next(error_codes),
|
||||
'message': 'This method is disabled while WROL Mode is enabled.',
|
||||
'status': HTTPStatus.FORBIDDEN,
|
||||
},
|
||||
ChannelURLEmpty: {
|
||||
'code': next(error_codes),
|
||||
'message': 'This channel has no URL',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
InvalidOTP: {
|
||||
'code': next(error_codes),
|
||||
'message': 'OTP has invalid characters',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
InvalidPlaintext: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Plaintext has invalid characters',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
InvalidCiphertext: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Ciphertext has invalid characters',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
NoFrequency: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Channel does not have a frequency',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
NoInventories: {
|
||||
'code': next(error_codes),
|
||||
'message': 'No Inventories',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
InventoriesVersionMismatch: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Inventories version in the DB does not match the inventories config',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
InvalidTimezone: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Invalid timezone',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
InvalidDomain: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Invalid archive domain',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
UnknownURL: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Unknown URL',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
PendingArchive: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Archive with that URL is already pending',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
InvalidArchive: {
|
||||
'code': next(error_codes),
|
||||
'message': 'The archive is invalid. See server logs.',
|
||||
'status': HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
},
|
||||
InvalidDownload: {
|
||||
'code': next(error_codes),
|
||||
'message': 'The URL cannot be downloaded.',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
ChannelSourceIdConflict: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Channel with source id already exists',
|
||||
'status': HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
},
|
||||
UnrecoverableDownloadError: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Download experienced an error which cannot be recovered. Download will be deleted.',
|
||||
'status': HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
},
|
||||
InvalidFile: {
|
||||
'code': next(error_codes),
|
||||
'message': 'File does not exist or is a directory',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
NativeOnly: {
|
||||
'code': next(error_codes),
|
||||
'message': 'This functionality is only supported outside a docker container',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
HotspotError: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Updating/accessing Hotspot encountered an error',
|
||||
'status': HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
},
|
||||
UnknownArchive: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Unable to find the archive',
|
||||
'status': HTTPStatus.NOT_FOUND,
|
||||
},
|
||||
UnknownTag: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Unable to find the tag',
|
||||
'status': HTTPStatus.NOT_FOUND,
|
||||
},
|
||||
UsedTag: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Tag is being used',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
InvalidTag: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Tag is not allowed',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
FileGroupIsTagged: {
|
||||
'code': next(error_codes),
|
||||
'message': 'File group is tagged',
|
||||
'status': HTTPStatus.CONFLICT,
|
||||
},
|
||||
FileUploadFailed: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Failed to upload file',
|
||||
'status': HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
UnknownZim: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Failed to find the Zim',
|
||||
'status': HTTPStatus.NOT_FOUND,
|
||||
},
|
||||
UnknownZimEntry: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Failed to find the Zim entry at that path',
|
||||
'status': HTTPStatus.NOT_FOUND,
|
||||
},
|
||||
UnknownZimTagEntry: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Failed to find the TagZimEntry',
|
||||
'status': HTTPStatus.NOT_FOUND,
|
||||
},
|
||||
UnknownZimSubscription: {
|
||||
'code': next(error_codes),
|
||||
'message': 'Failed to find the ZimSubscription',
|
||||
'status': HTTPStatus.NOT_FOUND,
|
||||
},
|
||||
FileConflict: {
|
||||
'code': next(error_codes),
|
||||
'message': 'File/directory already exists',
|
||||
'status': HTTPStatus.CONFLICT,
|
||||
},
|
||||
}
|
||||
class HotspotPasswordTooShort(APIError):
|
||||
code = 'HOTSPOT_PASSWORD_TOO_SHORT'
|
||||
summary = 'Hotspot password must be at least 8 characters'
|
||||
status = HTTPStatus.BAD_REQUEST
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ from unittest import mock
|
|||
|
||||
import pytest
|
||||
|
||||
from wrolpi.errors import API_ERRORS, WROLModeEnabled
|
||||
from wrolpi.errors import WROLModeEnabled
|
||||
from wrolpi.files import lib
|
||||
from wrolpi.files.models import FileGroup
|
||||
from wrolpi.tags import TagFile
|
||||
|
|
@ -153,7 +153,7 @@ async def test_delete_wrol_mode(test_async_client, wrol_mode_fixture):
|
|||
|
||||
request, response = await test_async_client.post('/api/files/delete', content=json.dumps({'paths': ['foo', ]}))
|
||||
assert response.status_code == HTTPStatus.FORBIDDEN
|
||||
assert response.json['code'] == API_ERRORS[WROLModeEnabled]['code']
|
||||
assert response.json['code'] == 'WROL_MODE_ENABLED'
|
||||
|
||||
|
||||
def test_files_search(test_session, test_client, make_files_structure, assert_files_search):
|
||||
|
|
|
|||
|
|
@ -24,7 +24,8 @@ from wrolpi.common import logger, get_config, wrol_mode_enabled, Base, get_media
|
|||
wrol_mode_check, native_only, disable_wrol_mode, enable_wrol_mode, get_global_statistics, url_strip_host
|
||||
from wrolpi.dates import now, strptime
|
||||
from wrolpi.downloader import download_manager
|
||||
from wrolpi.errors import WROLModeEnabled, API_ERRORS, APIError, ValidationError, HotspotError, InvalidDownload
|
||||
from wrolpi.errors import WROLModeEnabled, APIError, HotspotError, InvalidDownload, \
|
||||
HotspotPasswordTooShort
|
||||
from wrolpi.events import get_events, Events
|
||||
from wrolpi.files.lib import get_file_statistics, estimate_search
|
||||
from wrolpi.vars import API_HOST, API_PORT, DOCKERIZED, API_DEBUG, API_ACCESS_LOG, API_WORKERS, API_AUTO_RELOAD, \
|
||||
|
|
@ -175,6 +176,9 @@ def update_settings(_: Request, body: schema.SettingsRequest):
|
|||
enable_wrol_mode()
|
||||
return response.empty()
|
||||
|
||||
if body.hotspot_password and len(body.hotspot_password) < 8:
|
||||
raise HotspotPasswordTooShort()
|
||||
|
||||
# Remove any keys with None values, then save the config.
|
||||
config = {k: v for k, v in body.__dict__.items() if v is not None}
|
||||
wrolpi_config = get_config()
|
||||
|
|
@ -547,23 +551,31 @@ def json_response(*a, **kwargs) -> HTTPResponse:
|
|||
return resp
|
||||
|
||||
|
||||
def json_error_handler(request: Request, exception: Exception):
|
||||
error = API_ERRORS[type(exception)]
|
||||
if isinstance(exception, ValidationError):
|
||||
body = dict(error='Could not validate the contents of the request', code=error['code'])
|
||||
def get_error_json(exception: BaseException):
|
||||
"""Return a JSON representation of the Exception instance."""
|
||||
if isinstance(exception, APIError):
|
||||
# Error especially defined for WROLPi.
|
||||
body = dict(error=str(exception), summary=exception.summary, code=exception.code)
|
||||
else:
|
||||
body = dict(message=str(exception), api_error=error['message'], code=error['code'])
|
||||
if cause := exception.__cause__:
|
||||
try:
|
||||
cause = API_ERRORS[type(cause)]
|
||||
body['cause'] = dict(error=cause['message'], code=cause['code'])
|
||||
except KeyError:
|
||||
# Cause was not an APIError.
|
||||
logger.error(f'Could not find cause {cause}')
|
||||
logger.debug(f'API returning JSON error {exception=}'
|
||||
f' error={body.get("api_error") or body.get("error")}'
|
||||
f' code={body["code"]}')
|
||||
return json_response(body, error['status'])
|
||||
# Not a WROLPi APIError error.
|
||||
body = dict(
|
||||
error=str(exception),
|
||||
summary=None,
|
||||
code=None,
|
||||
)
|
||||
if exception.__cause__:
|
||||
# This exception was caused by another, follow the stack.
|
||||
body['cause'] = get_error_json(exception.__cause__)
|
||||
return body
|
||||
|
||||
|
||||
def json_error_handler(request: Request, exception: APIError):
|
||||
body = get_error_json(exception)
|
||||
error = repr(str(body["error"]))
|
||||
summary = repr(str(body["summary"]))
|
||||
code = body['code']
|
||||
logger.debug(f'API returning JSON error {exception=} {error=} {summary=} {code=}')
|
||||
return json_response(body, exception.status)
|
||||
|
||||
|
||||
api_app.error_handler.add(APIError, json_error_handler)
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ from mock import mock
|
|||
|
||||
from wrolpi.admin import HotspotStatus
|
||||
from wrolpi.common import get_config
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.downloader import Download, get_download_manager_config
|
||||
from wrolpi.root_api import api_app
|
||||
from wrolpi.errors import ValidationError, SearchEmpty
|
||||
from wrolpi.root_api import json_error_handler
|
||||
from wrolpi.test.common import skip_circleci, assert_dict_contains
|
||||
|
||||
|
||||
|
|
@ -193,7 +193,7 @@ def test_hotspot_settings(test_session, test_client, test_config):
|
|||
mock_admin.enable_hotspot.return_value = False
|
||||
request, response = test_client.patch('/api/settings', content=json.dumps({'hotspot_status': True}))
|
||||
assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR, response.json
|
||||
assert response.json['code'] == 35
|
||||
assert response.json['code'] == 'HOTSPOT_ERROR'
|
||||
mock_admin.enable_hotspot.assert_called_once()
|
||||
|
||||
# Turning off the hotspot succeeds.
|
||||
|
|
@ -207,7 +207,7 @@ def test_hotspot_settings(test_session, test_client, test_config):
|
|||
mock_admin.disable_hotspot.return_value = False
|
||||
request, response = test_client.patch('/api/settings', content=json.dumps({'hotspot_status': False}))
|
||||
assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR, response.json
|
||||
assert response.json['code'] == 35
|
||||
assert response.json['code'] == 'HOTSPOT_ERROR'
|
||||
mock_admin.disable_hotspot.assert_called_once()
|
||||
|
||||
mock_admin.enable_hotspot.reset_mock()
|
||||
|
|
@ -224,6 +224,16 @@ def test_hotspot_settings(test_session, test_client, test_config):
|
|||
# Changing the password restarts the hotspot.
|
||||
mock_admin.enable_hotspot.assert_called_once()
|
||||
|
||||
# Hotspot password must be at least 8 characters.
|
||||
mock_admin.disable_hotspot.return_value = True
|
||||
content = {'hotspot_password': '1234567', 'hotspot_ssid': 'new ssid'}
|
||||
request, response = test_client.patch('/api/settings', content=json.dumps(content))
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert response.json == {'code': 'HOTSPOT_PASSWORD_TOO_SHORT',
|
||||
'error': '',
|
||||
'summary': 'Hotspot password must be at least 8 characters'
|
||||
}
|
||||
|
||||
|
||||
@skip_circleci
|
||||
def test_throttle_toggle(test_session, test_client, test_config):
|
||||
|
|
@ -412,3 +422,42 @@ def test_search_str_estimate(test_session, test_client, test_directory, test_zim
|
|||
estimate=1,
|
||||
path=str(test_zim.path.relative_to(test_directory)),
|
||||
))
|
||||
|
||||
|
||||
def test_recursive_errors():
|
||||
"""Errors are reported recursively."""
|
||||
|
||||
def one():
|
||||
raise ValueError('Some error outside WROLPi')
|
||||
|
||||
def two():
|
||||
try:
|
||||
one()
|
||||
except Exception as e:
|
||||
raise SearchEmpty('A more specific error') from e
|
||||
|
||||
def three():
|
||||
try:
|
||||
two()
|
||||
except Exception as e:
|
||||
raise ValidationError('A broad error') from e
|
||||
|
||||
try:
|
||||
three()
|
||||
except Exception as e:
|
||||
response = json_error_handler(None, e)
|
||||
assert json.loads(response.body.decode()) == {
|
||||
'code': 'VALIDATION_ERROR',
|
||||
'error': 'A broad error',
|
||||
'summary': 'Could not validate the contents of the request',
|
||||
'cause': {
|
||||
'code': 'SEARCH_EMPTY',
|
||||
'error': 'A more specific error',
|
||||
'summary': 'Search is empty, search_str must have content.',
|
||||
'cause': {
|
||||
'code': None,
|
||||
'error': 'Some error outside WROLPi',
|
||||
'summary': None,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue