Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added implementation of DS_ErrorCodeToErrorMessage #2794

Merged
merged 3 commits into from
Feb 26, 2020

Conversation

imskr
Copy link
Collaborator

@imskr imskr commented Feb 26, 2020

Implementation of an API to get textual descriptions of error codes.

Fixes #2773

@community-tc-integration
Copy link

No Taskcluster jobs started for this pull request
The `allowPullRequests` configuration for this repository (in `.taskcluster.yml` on the
default branch) does not allow starting tasks for this pull request.

@imskr
Copy link
Collaborator Author

imskr commented Feb 26, 2020

@reuben I have added the function. Now, what should I add further to it?

Copy link
Contributor

@reuben reuben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I have a few suggestions for better phrasing, and the function needs to be documented in deepspeech.h.

native_client/deepspeech.cc Outdated Show resolved Hide resolved
native_client/deepspeech.cc Outdated Show resolved Hide resolved
native_client/deepspeech.cc Outdated Show resolved Hide resolved
native_client/deepspeech.h Show resolved Hide resolved
@kdavis-mozilla
Copy link
Contributor

@reuben @imskr Note that the way these strings are created, in static memory and not the heap, contradicts with the documentation of DS_FreeString() which says that it is used to "Free a char* string returned by the DeepSpeech API".

People will likely not catch this subtle difference and try to call DS_FreeString() on strings returned from this function.

@reuben
Copy link
Contributor

reuben commented Feb 26, 2020

Returning an allocated copy is unfortunate in this case because for getting the error string there are basically two main cases:

Case 1, abort entire program:

int err = DS_SomeAPI();
if (err != 0) {
    fprintf(stderr, "DeepSpeech error: %s\n", DS_ErrorCodeToErrorString(err));
    exit(1);
}

In this case freeing the string is pointless since you're exit-ing anyway.

Case 2, error propagation:

int err = DS_SomeAPI();
if (err != 0) {
    char* ds_copy = DS_ErrorCodeToErrorMessage(err);
    char* our_copy = strdup(ds_copy); // caller shouldn't have to know about DS API
    DS_FreeString(ds_copy);
    return MyErrorAbstraction(STT_ERROR, our_copy);
}

In this case, if we allocate the error string, we force applications to immediately copy it again with their own allocator, or we force them to include custom allocator/deleter support in their error abstraction, because otherwise some caller far away is going to have to know about DS_FreeString. Whereas a const C string can be propagated without this problem:

int err = DS_SomeAPI();
if (err != 0) {
    return MyErrorAbstraction(STT_ERROR, DS_ErrorCodeToErrorMessage(err));
}

@reuben
Copy link
Contributor

reuben commented Feb 26, 2020

Granted, this is also true for DS_Version where we went with that approach. Maybe consistency is worth the awkwardness cost, specially since most of our language bindings abstract this away.

@kdavis-mozilla
Copy link
Contributor

Yeah it's not ideal.

But having this mixed API where some strings need to be freed with

DS_FreeString()

and some strings do not need to be freed with

DS_FreeString()

is also not ideal as it's confusing.

As for Case1, your example is true of very simply programs and applications, but for production integrations, think a GUI based application, you would never just exit if someone encountered a DS_ERR_INVALID_ALPHABET error. You'd "catch" the error and present them with a dialog to specify a correct alphabet file.

@imskr
Copy link
Collaborator Author

imskr commented Feb 26, 2020

How should I proceed with DS_FreeString() in the DS_ErrorCodeToErrorMessage() ?

@reuben
Copy link
Contributor

reuben commented Feb 26, 2020

@imskr please return a copy of the string using strdup and change the documentation accordingly to mention that people should use DS_FreeString on the returned pointer.

@imskr
Copy link
Collaborator Author

imskr commented Feb 26, 2020

Okay, thank you @reuben @kdavis-mozilla

@imskr
Copy link
Collaborator Author

imskr commented Feb 26, 2020

Is it good now? @reuben

reuben added a commit that referenced this pull request Feb 26, 2020
@reuben reuben merged commit 03196c8 into mozilla:master Feb 26, 2020
@imskr imskr deleted the sk-fix-api branch February 26, 2020 14:21
@reuben
Copy link
Contributor

reuben commented Feb 26, 2020

@imskr thanks for the PR!

@lock
Copy link

lock bot commented Mar 27, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Good First Bug] Implement an API to get textual descriptions of error codes
3 participants