Wrapping most API requests in trys/catches.

This commit is contained in:
Roland 2022-05-01 21:03:30 -06:00
parent 8ab61bd8a8
commit 6a654cd3af
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
10 changed files with 355 additions and 123 deletions

View file

@ -63,19 +63,23 @@ class Settings extends React.Component {
}
async componentDidMount() {
const settings = await getSettings();
this.setState({
ready: true,
disabled: settings.wrol_mode,
download_on_startup: settings.download_on_startup,
hotspot_on_startup: settings.hotspot_on_startup,
hotspot_password: settings.hotspot_password,
hotspot_ssid: settings.hotspot_ssid,
hotspot_status: settings.hotspot_status,
throttle_on_startup: settings.throttle_on_startup,
throttle_status: settings.throttle_status,
timezone: {value: settings.timezone, label: settings.timezone},
}, this.handleHotspotChange);
try {
const settings = await getSettings();
this.setState({
ready: true,
disabled: settings.wrol_mode,
download_on_startup: settings.download_on_startup,
hotspot_on_startup: settings.hotspot_on_startup,
hotspot_password: settings.hotspot_password,
hotspot_ssid: settings.hotspot_ssid,
hotspot_status: settings.hotspot_status,
throttle_on_startup: settings.throttle_on_startup,
throttle_status: settings.throttle_status,
timezone: {value: settings.timezone, label: settings.timezone},
}, this.handleHotspotChange);
} catch (e) {
console.error(e);
}
}
async handleSubmit(e) {
@ -92,7 +96,9 @@ class Settings extends React.Component {
}
handleInputChange = async (e, name, value) => {
if (e) {e.preventDefault()}
if (e) {
e.preventDefault()
}
this.setState({[name]: value});
}
@ -221,8 +227,12 @@ class WROLMode extends React.Component {
}
async componentDidMount() {
let settings = await getSettings();
this.setState({ready: true, WROLMode: settings.wrol_mode});
try {
let settings = await getSettings();
this.setState({ready: true, WROLMode: settings.wrol_mode});
} catch (e) {
console.error(e);
}
}
toggleWROLMode = async () => {
@ -464,12 +474,16 @@ class Downloads extends React.Component {
}
fetchDownloads = async () => {
let data = await getDownloads();
this.setState({
once_downloads: data.once_downloads,
recurring_downloads: data.recurring_downloads,
pending_downloads: data.pending_downloads,
});
try {
let data = await getDownloads();
this.setState({
once_downloads: data.once_downloads,
recurring_downloads: data.recurring_downloads,
pending_downloads: data.pending_downloads,
});
} catch (e) {
console.error(e);
}
}
render() {

View file

@ -17,8 +17,12 @@ class Encrypt extends React.Component {
handleSubmit = async (e) => {
e.preventDefault();
let {otp, plaintext, ciphertext} = await encryptOTP(this.state.otp, this.state.plaintext);
this.setState({otp, plaintext, ciphertext});
try {
let {otp, plaintext, ciphertext} = await encryptOTP(this.state.otp, this.state.plaintext);
this.setState({otp, plaintext, ciphertext});
} catch (e) {
console.error(e);
}
}
handleInputChange = async (event, {name, value}) => {
@ -74,8 +78,12 @@ class Decrypt extends React.Component {
handleSubmit = async (e) => {
e.preventDefault();
let {otp, plaintext, ciphertext} = await decryptOTP(this.state.otp, this.state.ciphertext);
this.setState({otp, plaintext, ciphertext});
try {
let {otp, plaintext, ciphertext} = await decryptOTP(this.state.otp, this.state.ciphertext);
this.setState({otp, plaintext, ciphertext});
} catch (e) {
console.error(e);
}
}
handleInputChange = async (event, {name, value}) => {

View file

@ -18,7 +18,8 @@ import {
downloadChannel,
getChannel,
getChannels,
getDirectories, getSettings,
getDirectories,
getSettings,
refreshChannel,
updateChannel,
validateRegex
@ -565,8 +566,14 @@ export class Channels extends React.Component {
}
async componentDidMount() {
let channels = await getChannels();
this.setState({channels, results: channels});
try {
let channels = await getChannels();
if (channels) {
this.setState({channels, results: channels});
}
} catch (e) {
console.error(e);
}
}
handleSearchChange = async (e, {value}) => {

View file

@ -570,8 +570,14 @@ export const useWROLMode = () => {
const [enabled, setEnabled] = useState(false);
const fetchStatus = async () => {
let settings = await getSettings();
setEnabled(settings.wrol_mode);
try {
const settings = await getSettings();
if (settings) {
setEnabled(settings.wrol_mode);
}
} catch (e) {
console.error(e);
}
}
useEffect(() => {

View file

@ -37,11 +37,15 @@ class InventorySummary extends React.Component {
fetchInventory = async () => {
if (this.state.inventory) {
let {by_category, by_subcategory, by_name} = await getInventory(this.state.inventory.id);
by_category = enumerate(by_category);
by_subcategory = enumerate(by_subcategory);
by_name = enumerate(by_name);
this.setState({by_category, by_subcategory, by_name});
try {
let {by_category, by_subcategory, by_name} = await getInventory(this.state.inventory.id);
by_category = enumerate(by_category);
by_subcategory = enumerate(by_subcategory);
by_name = enumerate(by_name);
this.setState({by_category, by_subcategory, by_name});
} catch (e) {
console.error(e);
}
}
}
@ -611,20 +615,24 @@ class InventorySelector extends React.Component {
}
async fetchInventories() {
let inventories = await getInventories();
let options = [];
for (let i = 0; i < inventories.length; i++) {
let inventory = inventories[i];
options = options.concat([{
key: inventory.id,
text: inventory.name,
value: inventory.id,
id: inventory.id,
}])
try {
let inventories = await getInventories();
let options = [];
for (let i = 0; i < inventories.length; i++) {
let inventory = inventories[i];
options = options.concat([{
key: inventory.id,
text: inventory.name,
value: inventory.id,
id: inventory.id,
}])
}
let selected = this.state.selected || inventories[0];
this.setState({inventories, options, selected},
() => this.props.setInventory(selected));
} catch (e) {
console.error(e);
}
let selected = this.state.selected || inventories[0];
this.setState({inventories, options, selected},
() => this.props.setInventory(selected));
}
setInventory = (e, {value}) => {
@ -748,15 +756,19 @@ class CategoryInputs extends React.Component {
}
fetchCategories = async () => {
let categories = await getCategories();
categories = enumerate(categories);
let newCategories = [];
for (let i = 0; i < categories.length; i++) {
let [key, sc] = categories[i];
let [subcategory, category] = sc;
newCategories = newCategories.concat([[key, subcategory, `${subcategory}/${category}`]]);
try {
let categories = await getCategories();
categories = enumerate(categories);
let newCategories = [];
for (let i = 0; i < categories.length; i++) {
let [key, sc] = categories[i];
let [subcategory, category] = sc;
newCategories = newCategories.concat([[key, subcategory, `${subcategory}/${category}`]]);
}
this.setState({categories: newCategories});
} catch (e) {
console.error(e);
}
this.setState({categories: newCategories});
}
clearInputs = () => {
@ -858,14 +870,18 @@ class BrandInput extends React.Component {
}
fetchBrands = async () => {
let brands = await getBrands();
brands = enumerate(brands);
let newBrands = [];
for (let i = 0; i < brands.length; i++) {
let [key, brand] = brands[i];
newBrands = newBrands.concat([[key, brand, brand]]);
try {
let brands = await getBrands();
brands = enumerate(brands);
let newBrands = [];
for (let i = 0; i < brands.length; i++) {
let [key, brand] = brands[i];
newBrands = newBrands.concat([[key, brand, brand]]);
}
this.setState({options: newBrands});
} catch (e) {
console.error(e);
}
this.setState({options: newBrands});
}
handleInputChange = (e, {name, value}) => {

View file

@ -1,5 +1,5 @@
import React from "react";
import {HelpPopup, humanFileSize, minutesToTimestamp, PageContainer, TabLinks, WROLModeMessage} from "./Common";
import {HelpPopup, humanFileSize, PageContainer, TabLinks, WROLModeMessage} from "./Common";
import {Route} from "react-router-dom";
import {getMapImportStatus, importMapFiles} from "../api";
import Table from "semantic-ui-react/dist/commonjs/collections/Table";
@ -28,13 +28,17 @@ class ManageMap extends React.Component {
}
fetchImportStatus = async () => {
const importStatus = await getMapImportStatus();
this.setState({
files: importStatus['files'],
importing: importStatus['importing'],
import_running: importStatus['import_running'],
dockerized: importStatus['dockerized'],
});
try {
const importStatus = await getMapImportStatus();
this.setState({
files: importStatus['files'],
importing: importStatus['importing'],
import_running: importStatus['import_running'],
dockerized: importStatus['dockerized'],
});
} catch (e) {
console.error(e);
}
}
import = async (e) => {

View file

@ -306,8 +306,13 @@ class Videos extends React.Component {
// Pass any enabled filters to the search.
let filtersArr = this.enabledFilters();
let [videos, total] = await searchVideos(offset, limit, channel_id, queryStr, searchOrder, filtersArr);
let videos = [];
let total = 0;
try {
let [videos, total] = await searchVideos(offset, limit, channel_id, queryStr, searchOrder, filtersArr);
} catch (e) {
console.error(e);
}
let totalPages = Math.round(total / this.state.limit) || 1;
this.setState({videos, totalPages});
}
@ -573,12 +578,16 @@ class Statistics extends React.Component {
}
async fetchStatistics() {
let stats = await getStatistics();
stats.videos.sum_duration = secondsToString(stats.videos.sum_duration);
stats.videos.sum_size = humanFileSize(stats.videos.sum_size, true);
stats.videos.max_size = humanFileSize(stats.videos.max_size, true);
stats.historical.average_size = humanFileSize(stats.historical.average_size, true);
this.setState({...stats});
try {
let stats = await getStatistics();
stats.videos.sum_duration = secondsToString(stats.videos.sum_duration);
stats.videos.sum_size = humanFileSize(stats.videos.sum_size, true);
stats.videos.max_size = humanFileSize(stats.videos.max_size, true);
stats.historical.average_size = humanFileSize(stats.historical.average_size, true);
this.setState({...stats});
} catch (e) {
console.error(e);
}
}
buildSegment(title, names, stats) {