CockpitApiError.fromJson constructor

CockpitApiError.fromJson(
  1. Object? value, {
  2. String path = r'$',
  3. CockpitDecodePolicy decodePolicy = CockpitDecodePolicy.requests,
})

Decodes a CockpitApiError from a JSON object.

Implementation

factory CockpitApiError.fromJson(
  Object? value, {
  String path = r'$',
  CockpitDecodePolicy decodePolicy = CockpitDecodePolicy.requests,
}) {
  final json = CockpitFoundationValueReader.object(value, path);
  CockpitFoundationValueReader.keys(
    json,
    const <String>{
      'code',
      'category',
      'message',
      'retryable',
      'responsibleLayer',
      'redactedDetails',
      'artifacts',
    },
    path,
    required: const <String>{
      'code',
      'category',
      'message',
      'retryable',
      'responsibleLayer',
    },
    policy: decodePolicy,
  );
  final rawArtifacts = json['artifacts'] == null
      ? const <Object?>[]
      : CockpitFoundationValueReader.list(
          json['artifacts'],
          '$path.artifacts',
        );
  return CockpitApiError(
    code: CockpitFoundationValueReader.id(json['code'], '$path.code'),
    category: _closedEnum(
      json['category'],
      CockpitErrorCategory.values,
      '$path.category',
    ),
    message: CockpitFoundationValueReader.string(
      json['message'],
      '$path.message',
      maximum: 4096,
    ),
    retryable: CockpitFoundationValueReader.boolean(
      json['retryable'],
      '$path.retryable',
    ),
    responsibleLayer: _closedEnum(
      json['responsibleLayer'],
      CockpitResponsibleLayer.values,
      '$path.responsibleLayer',
    ),
    redactedDetails: json['redactedDetails'] == null
        ? const <String, Object?>{}
        : CockpitFoundationValueReader.jsonObject(
            json['redactedDetails'],
            '$path.redactedDetails',
          ),
    artifacts: <CockpitArtifactReference>[
      for (var index = 0; index < rawArtifacts.length; index += 1)
        CockpitArtifactReference.fromJson(
          rawArtifacts[index],
          path: '$path.artifacts[$index]',
          decodePolicy: decodePolicy,
        ),
    ],
  );
}