Skip to content

Custom Errors

Service code may return custom error code and/or error message using the API documented below. The API also supports returning an HTTP status code along with the error code/message. The following sample service demonstrates possible usages of the custom error API:

class ErrorExampleService {
  simpleError() {
    //error code:0
    //error message: 'I am a simple Error'
    //httpStatusCode: -1 (default)
    throw new Error('I am a simple Error');
  }

  customError() {
    //error code:245
    //error message: 'I am a specific error'
    //httpStatusCode: -1 (default)
    throw new Backendless.ServerCode.Error(245, 'I am a specific error');
  }

  customErrorWithHttpStatusCode() {
    //error code:245
    //error message: 'I am a specific error'
    //httpStatusCode: 403
    throw new Backendless.ServerCode.Error(245, 'I am a specific error', 403);
  }

}

Backendless.ServerCode.addService(ErrorExampleService);