1:- module(bc_api_error, [
    2    bc_call_handle_error/1, % :Goal
    3    bc_handle_error/1       % +Error
    4]).    5
    6:- use_module(bc_api_io).
 bc_call_handle_error(:Goal) is det
Calls handler through error cather that produces JSON reply on recognized errors.
   13:- meta_predicate(bc_call_handle_error(0)).   14
   15bc_call_handle_error(Goal):-
   16    catch(Goal, Error, true),
   17    (   var(Error)
   18    ;   bc_handle_error(Error)), !.
 bc_handle_error(+Error) is det
Handles known errors and produces approriate API response.
   25bc_handle_error(error(invalid_api_key)):- !,
   26    bc_reply_error('Invalid or missing API key.').
   27
   28bc_handle_error(error(invalid_input(Errors))):- !,
   29    format(atom(Message), 'Invalid input: ~w.', [Errors]),
   30    bc_reply_error(Message).
   31
   32bc_handle_error(error(no_config(Name))):- !,
   33    format(atom(Message), 'Invalid config key: ~w.', [Name]),
   34    bc_reply_error(Message).
   35
   36bc_handle_error(error(invalid_credentials)):- !,
   37    bc_reply_error('Invalid auth credentials.').
   38
   39bc_handle_error(error(no_login_access)):- !,
   40    bc_reply_error('Assigned role does not permit login.').
   41
   42bc_handle_error(error(invalid_username)):- !,
   43    bc_reply_error('The username is invalid.').
   44
   45bc_handle_error(error(existing_username)):- !,
   46    bc_reply_error('The username exists.').
   47
   48bc_handle_error(error(invalid_role)):- !,
   49    bc_reply_error('The user role is not valid.').
   50
   51bc_handle_error(error(user_not_exists)):- !,
   52    bc_reply_error('The user does not exist.').
   53
   54bc_handle_error(error(has_entries)):- !,
   55    bc_reply_error('The user has entries.').
   56
   57bc_handle_error(error(no_remaining_admin)):- !,
   58    bc_reply_error('Cannot remove the last admin.').
   59
   60bc_handle_error(error(existing_slug)):- !,
   61    bc_reply_error('The entry with the same slug exists already.').
   62
   63bc_handle_error(error(entry_not_exists)):- !,
   64    bc_reply_error('The entry does not exist.').
   65
   66bc_handle_error(error(no_access)):- !,
   67    bc_reply_error('The operation requires access privileges.').
   68
   69bc_handle_error(error(unsafe_path(_))):- !,
   70    bc_reply_error('The file/directory path is unsafe.').
   71
   72bc_handle_error(error(directory_exists)):- !,
   73    bc_reply_error('The directory exists.').
   74
   75bc_handle_error(error(file_exists)):- !,
   76    bc_reply_error('The file exists.').
   77
   78bc_handle_error(error(incorrect_answer)):- !,
   79    bc_reply_error('The human question answer is wrong.').
   80
   81bc_handle_error(error(no_comments_allowed)):- !,
   82    bc_reply_error('No comments are allowed on the entry.').
   83
   84bc_handle_error(error(comment_not_exists)):- !,
   85    bc_reply_error('The comment replied to does not exist.').
   86
   87bc_handle_error(error(invalid_slug)):- !,
   88    bc_reply_error('The slug is invalid.').
   89
   90bc_handle_error(error(no_action)):- !,
   91    bc_reply_error('No such action exists.').
   92
   93bc_handle_error(error(action_failed)):- !,
   94    bc_reply_error('The action failed to execute.').
   95
   96bc_handle_error(Error):-
   97    throw(Error)