fix(core): report config error when external media lib is missing (#3255)

This commit is contained in:
Erez Rokah 2020-02-14 22:32:36 +02:00 committed by GitHub
parent 02ef2010e7
commit 1d63038e78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 6 deletions

View File

@ -5,6 +5,7 @@
import { once } from 'lodash'; import { once } from 'lodash';
import { getMediaLibrary } from './lib/registry'; import { getMediaLibrary } from './lib/registry';
import store from './redux'; import store from './redux';
import { configFailed } from './actions/config';
import { createMediaLibrary, insertMedia } from './actions/mediaLibrary'; import { createMediaLibrary, insertMedia } from './actions/mediaLibrary';
import { MediaLibraryInstance } from './types/redux'; import { MediaLibraryInstance } from './types/redux';
@ -18,10 +19,17 @@ interface MediaLibrary {
} }
const initializeMediaLibrary = once(async function initializeMediaLibrary(name, options) { const initializeMediaLibrary = once(async function initializeMediaLibrary(name, options) {
const lib = (getMediaLibrary(name) as unknown) as MediaLibrary; const lib = (getMediaLibrary(name) as unknown) as MediaLibrary | undefined;
if (!lib) {
const err = new Error(
`Missing external media library '${name}'. Please use 'registerMediaLibrary' to register it.`,
);
store.dispatch(configFailed(err));
} else {
const handleInsert = (url: string) => store.dispatch(insertMedia(url, undefined)); const handleInsert = (url: string) => store.dispatch(insertMedia(url, undefined));
const instance = await lib.init({ options, handleInsert }); const instance = await lib.init({ options, handleInsert });
store.dispatch(createMediaLibrary(instance)); store.dispatch(createMediaLibrary(instance));
}
}); });
store.subscribe(() => { store.subscribe(() => {

View File

@ -3,7 +3,9 @@ import { CONFIG_REQUEST, CONFIG_SUCCESS, CONFIG_FAILURE, CONFIG_MERGE } from '..
import { Config, ConfigAction } from '../types/redux'; import { Config, ConfigAction } from '../types/redux';
import { EDITORIAL_WORKFLOW } from '../constants/publishModes'; import { EDITORIAL_WORKFLOW } from '../constants/publishModes';
const config = (state = Map({ isFetching: true }), action: ConfigAction) => { const defaultState: Map<string, boolean | string> = Map({ isFetching: true });
const config = (state = defaultState, action: ConfigAction) => {
switch (action.type) { switch (action.type) {
case CONFIG_MERGE: case CONFIG_MERGE:
return state.mergeDeep(action.payload); return state.mergeDeep(action.payload);
@ -17,7 +19,10 @@ const config = (state = Map({ isFetching: true }), action: ConfigAction) => {
*/ */
return action.payload.delete('isFetching'); return action.payload.delete('isFetching');
case CONFIG_FAILURE: case CONFIG_FAILURE:
return Map({ error: action.payload.toString() }); return state.withMutations(s => {
s.delete('isFetching');
s.set('error', action.payload.toString());
});
default: default:
return state; return state;
} }

View File

@ -41,6 +41,7 @@ export type Config = StaticallyTypedRecord<{
site_id?: string; site_id?: string;
site_url?: string; site_url?: string;
show_preview_links?: boolean; show_preview_links?: boolean;
isFetching?: boolean;
}>; }>;
type PagesObject = { type PagesObject = {