Handling Errors
The Metronome API has strict input data checking. When the service cannot validate the input data, it will respond with specific information regarding which fields are incorrect, missing, or which contain invalid information. These are called schema validation errors. It is also possible to make other errors, such as using a key which does not have the correct API permissions, which will receive an authorisation error.
You might consider describing errors from Metronome in the following way. This code will be considered included in the following examples.
import { ApiException, ValidationError, ValidationProblem } from "@privatedataservices/metronome-client";
export const explainError = (err: any) => { if (err instanceof ApiException) { err.body.errors.forEach( (error: any, errorIndex: number) => { if(error instanceof String) console.error("Error %s: ", errorIndex, error); else { var validationError = error as ValidationError; console.error(" Error %s is a validation error.The server side spec that failed to match is '%s' and the request received from this client at the server was: %s", errorIndex, validationError.spec, JSON.stringify(validationError.requestFromClient)); validationError.problems.forEach( (problem: ValidationProblem) => { console.log(" Problem location: %s", problem.location.join(", ")); console.log(" Problem value: %s", problem.val); console.log(" Problem description: %s", problem.description); }); } }); } else { console.error("Non-API exception:", err); }}require 'metronome'
def explain_error(err) if err.is_a?(Metronome::ApiError) begin error_body = JSON.parse(err.response_body) if err.response_body if error_body && error_body['errors'] error_body['errors'].each_with_index do |error, error_index| if error.is_a?(String) puts "Error #{error_index}: #{error}" else puts "Error #{error_index} is a validation error. The server side spec that failed to match is '#{error['spec']}' and the request received from this client at the server was: #{error['requestFromClient'].to_json}" if error['problems'] error['problems'].each do |problem| puts " Problem location: #{problem['location'].join(', ')}" puts " Problem value: #{problem['val']}" puts " Problem description: #{problem['description']}" end end end end else puts "API Error: #{err.message}" puts "Response body: #{err.response_body}" if err.response_body end rescue JSON::ParserError puts "API Error: #{err.message}" puts "Response body: #{err.response_body}" if err.response_body end else puts "Non-API exception: #{err.class} - #{err.message}" endend