diff --git a/pages/api/ibge/municipios/v1/[uf].js b/pages/api/ibge/municipios/v1/[uf].js index d7afa7122..e1256d111 100644 --- a/pages/api/ibge/municipios/v1/[uf].js +++ b/pages/api/ibge/municipios/v1/[uf].js @@ -9,13 +9,14 @@ import InternalError from '@/errors/InternalError'; import BaseError from '@/errors/BaseError'; import { getCities } from '@/services/dados-abertos-br/cities'; +import { rejectWhenEmptyArray } from '@/services/util/rejectWhenEmptyArray'; const getData = async (uf, providers = null) => { const promises = []; if (!providers) { - promises.push(getContiesByUf(uf)); - promises.push(getCities(uf)); - promises.push(getStateCities(uf)); + promises.push(rejectWhenEmptyArray(getContiesByUf(uf))); + promises.push(rejectWhenEmptyArray(getCities(uf))); + promises.push(rejectWhenEmptyArray(getStateCities(uf))); } else { if (providers.includes('wikipedia')) { promises.push(getStateCities(uf)); diff --git a/services/util/rejectWhenEmptyArray.js b/services/util/rejectWhenEmptyArray.js new file mode 100644 index 000000000..bebc7bf5d --- /dev/null +++ b/services/util/rejectWhenEmptyArray.js @@ -0,0 +1,13 @@ +/** + * Rejects the promise if the data is an empty array. + * + * @param {Promise} promise + * @returns {Promise} + */ +export async function rejectWhenEmptyArray(promise) { + const data = await promise; + if (!data || data.length === 0) { + throw new Error('Empty data'); + } + return data; +}