open-nomad/ui/app/utils/json-with-default.js
2023-04-10 15:36:59 +00:00

16 lines
435 B
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { copy } from 'ember-copy';
// Used with fetch.
// Fetch only goes into the promise catch if there is a network error.
// This means that handling a 4xx or 5xx error is the responsibility
// of the developer.
const jsonWithDefault = (defaultResponse) => (res) =>
res.ok ? res.json() : copy(defaultResponse, true);
export default jsonWithDefault;