4444 | {{ $t('settings.categorization.restoreDefaults') }}
4545 label.btn.btn-sm.ml-1.btn-outline-primary ( style ="margin: 0" )
4646 | {{ $t('common.import') }}
47- input( type ="file" @change ="importCategories" hidden )
47+ input( type ="file" accept = ".json,application/json" @change ="importCategories" hidden )
4848 b-btn.ml-1 ( @click ="exportClasses" , variant ="outline-primary" size ="sm" )
4949 | {{ $t('common.export') }}
5050
@@ -97,6 +97,7 @@ import 'vue-awesome/icons/angle-double-up';
9797import { useCategoryStore } from ' ~/stores/categories' ;
9898
9999import { downloadFile } from ' ~/util/export' ;
100+ import { parseCategoryImport , shouldAttemptJsonImport } from ' ~/util/importFile' ;
100101
101102export default {
102103 name: ' CategorizationSettings' ,
@@ -155,7 +156,17 @@ export default {
155156 this .editingId = lastId ;
156157 },
157158 saveClasses : async function () {
158- await this .categoryStore .save ();
159+ try {
160+ await this .categoryStore .save ();
161+ } catch (e ) {
162+ console .error (' Failed to save categories' , e );
163+ const httpStatus = e && e .response && e .response .status ;
164+ const detail = (e && e .message ) || String (e );
165+ const prefix = httpStatus
166+ ? ` Failed to save categories (HTTP ${httpStatus }) `
167+ : ' Failed to save categories' ;
168+ alert (` ${prefix }: ${detail } ` );
169+ }
159170 },
160171 resetClasses : async function () {
161172 await this .categoryStore .load ();
@@ -174,13 +185,23 @@ export default {
174185 },
175186 importCategories : async function (elem ) {
176187 const file = elem .target .files [0 ];
177- if (file .type != ' application/json' ) {
178- console .error (' Only JSON files are possible to import' );
188+ if (! file ) return ;
189+ // Reset so picking the same file again retriggers change.
190+ elem .target .value = ' ' ;
191+
192+ if (! shouldAttemptJsonImport (file )) {
193+ alert (' Please select a JSON category export, not an image or other file type.' );
179194 return ;
180195 }
181196
182- const text = await file .text ();
183- const import_obj = JSON .parse (text );
197+ let import_obj;
198+ try {
199+ import_obj = parseCategoryImport (await file .text ());
200+ } catch (e ) {
201+ console .error (' Failed to parse category import' , e );
202+ alert (' Could not import categories: file is not a valid JSON category export.' );
203+ return ;
204+ }
184205
185206 if (import_obj .categories && ! import_obj .id ) {
186207 this .categoryStore .import (import_obj .categories );
@@ -207,8 +228,6 @@ export default {
207228 this .categoryStore .switchToSet (setId );
208229 }
209230 this .categoryStore .classes_unsaved_changes = true ;
210- } else {
211- console .error (' Unrecognized import format' );
212231 }
213232 },
214233 createSet : function () {
0 commit comments