Cscott has uploaded a new change for review.
https://gerrit.wikimedia.org/r/206481
Change subject: Factor out getConfigPromise() method.
......................................................................
Factor out getConfigPromise() method.
Use `Promise.method` to ensure that any synchronous exceptions thrown
(either intentionally or by programmer error) get turned into rejected
Promises. This also lets us use synchronous return/throw in the body for
readability. Using return/throw also makes the control flow clearer,
ensuring that future maintainers don't have to worry about multiple
assignment.
Change-Id: I4d7bf027b9cc09b24859f6cf9dae3290e6e2530f
---
M lib/mediawiki.parser.environment.js
1 file changed, 34 insertions(+), 34 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid
refs/changes/81/206481/1
diff --git a/lib/mediawiki.parser.environment.js
b/lib/mediawiki.parser.environment.js
index c3598a5..067848c 100644
--- a/lib/mediawiki.parser.environment.js
+++ b/lib/mediawiki.parser.environment.js
@@ -278,45 +278,45 @@
var nothingToDo = {}; // unique marker value
var parsoid = env.conf.parsoid;
- var getConfigPromise;
- if (!prefix) {
- getConfigPromise = Promise.reject(new Error('Wiki prefix not
provided'));
- } else {
- var uri = parsoid.interwikiMap.get(prefix);
- if (!uri) {
- // SSS: Ugh! Looks like parser tests use a prefix
- // that is not part of the interwikiMap -- so we
- // cannot crash with an error. Hence defaulting
- // to enwiki api which is quite odd. Does the
- // interwikiMap need updating or is this use-case
- // valid outside of parserTests??
- env.log('error', 'Did not find api uri for ' +
- prefix + '; defaulting to enwiki');
- uri = parsoid.interwikiMap.get('enwiki');
- }
- parsoid.apiURI = uri;
-
- if (env.confCache[prefix]) {
- env.conf.wiki = env.confCache[prefix];
- getConfigPromise = Promise.resolve(nothingToDo);
- } else if (parsoid.fetchConfig) {
- getConfigPromise = ConfigRequest
- .promise(uri, env, env.getAPIProxy(prefix));
+ var getConfigPromise = Promise.method(function() {
+ if (!prefix) {
+ throw new Error('Wiki prefix not provided');
} else {
- // Load the config from cached config on disk
- var localConfigFile = './baseconfig/' + prefix +
'.json';
- var localConfig = require(localConfigFile);
- if (localConfig && localConfig.query) {
- getConfigPromise =
Promise.resolve(localConfig.query);
+ var uri = parsoid.interwikiMap.get(prefix);
+ if (!uri) {
+ // SSS: Ugh! Looks like parser tests use a
prefix
+ // that is not part of the interwikiMap -- so we
+ // cannot crash with an error. Hence defaulting
+ // to enwiki api which is quite odd. Does the
+ // interwikiMap need updating or is this
use-case
+ // valid outside of parserTests??
+ env.log('error', 'Did not find api uri for ' +
+ prefix + '; defaulting to
enwiki');
+ uri = parsoid.interwikiMap.get('enwiki');
+ }
+ parsoid.apiURI = uri;
+
+ if (env.confCache[prefix]) {
+ env.conf.wiki = env.confCache[prefix];
+ return nothingToDo;
+ } else if (parsoid.fetchConfig) {
+ return ConfigRequest
+ .promise(uri, env,
env.getAPIProxy(prefix));
} else {
- getConfigPromise = Promise.reject(
- new Error('Could not read valid config
from file: ' +
- localConfigFile));
+ // Load the config from cached config on disk
+ var localConfigFile = './baseconfig/' + prefix
+ '.json';
+ var localConfig = require(localConfigFile);
+ if (localConfig && localConfig.query) {
+ return localConfig.query;
+ } else {
+ throw new Error('Could not read valid
config from file: ' +
+ localConfigFile);
+ }
}
}
- }
+ });
- return getConfigPromise.then(function(resultConf) {
+ return getConfigPromise().then(function(resultConf) {
if (resultConf === nothingToDo) { return; }
env.conf.wiki = new WikiConfig(env, resultConf, prefix,
parsoid.apiURI,
env.getAPIProxy(prefix));
--
To view, visit https://gerrit.wikimedia.org/r/206481
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4d7bf027b9cc09b24859f6cf9dae3290e6e2530f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits