Skip to content

Commit

Permalink
Merge pull request mozilla#2970 from mozilla/enabledecoder-error-hand…
Browse files Browse the repository at this point in the history
…ling

Improve error handling for DS_EnableExternalScorer (Fixes mozilla#2969)
  • Loading branch information
reuben authored May 4, 2020
2 parents fdee05f + 4897141 commit d36092c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
5 changes: 3 additions & 2 deletions native_client/deepspeech.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,12 @@ int
DS_EnableExternalScorer(ModelState* aCtx,
const char* aScorerPath)
{
aCtx->scorer_.reset(new Scorer());
int err = aCtx->scorer_->init(aScorerPath, aCtx->alphabet_);
std::unique_ptr<Scorer> scorer(new Scorer());
int err = scorer->init(aScorerPath, aCtx->alphabet_);
if (err != 0) {
return DS_ERR_INVALID_SCORER;
}
aCtx->scorer_ = std::move(scorer);
return DS_ERR_OK;
}

Expand Down
11 changes: 7 additions & 4 deletions native_client/javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Model(aModelPath) {
const status = rets[0];
const impl = rets[1];
if (status !== 0) {
throw "CreateModel failed "+binding.ErrorCodeToErrorMessage(status)+" 0x" + status.toString(16);
throw "CreateModel failed with '"+binding.ErrorCodeToErrorMessage(status)+"' (0x" + status.toString(16) + ")";
}

this._impl = impl;
Expand Down Expand Up @@ -76,10 +76,13 @@ Model.prototype.sampleRate = function() {
*
* @param {string} aScorerPath The path to the external scorer file.
*
* @return {number} Zero on success, non-zero on failure (invalid arguments).
* @throws on error
*/
Model.prototype.enableExternalScorer = function(aScorerPath) {
return binding.EnableExternalScorer(this._impl, aScorerPath);
const status = binding.EnableExternalScorer(this._impl, aScorerPath);
if (status !== 0) {
throw "EnableExternalScorer failed with '"+binding.ErrorCodeToErrorMessage(status)+"' (0x" + status.toString(16) + ")";
}
}

/**
Expand Down Expand Up @@ -139,7 +142,7 @@ Model.prototype.createStream = function() {
const status = rets[0];
const ctx = rets[1];
if (status !== 0) {
throw "CreateStream failed "+binding.ErrorCodeToErrorMessage(status)+" 0x" + status.toString(16);
throw "CreateStream failed with '"+binding.ErrorCodeToErrorMessage(status)+"' (0x" + status.toString(16) + ")";
}
return new Stream(ctx);
}
Expand Down
7 changes: 4 additions & 3 deletions native_client/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ def enableExternalScorer(self, scorer_path):
:param scorer_path: The path to the external scorer file.
:type scorer_path: str
:return: Zero on success, non-zero on failure.
:type: int
:throws: RuntimeError on error
"""
return deepspeech.impl.EnableExternalScorer(self._impl, scorer_path)
status = deepspeech.impl.EnableExternalScorer(self._impl, scorer_path)
if status != 0:
raise RuntimeError("EnableExternalScorer failed with '{}' (0x{:X})".format(deepspeech.impl.ErrorCodeToErrorMessage(status),status))

def disableExternalScorer(self):
"""
Expand Down

0 comments on commit d36092c

Please sign in to comment.