cami-la / sacola-api_IFOOD_DEV_WEEK

Código-Fonte do Projeto Sacola API do iFood Dev Week.
https://www.dio.me/dev-week/ifood/ifood-developer
167 stars 29 forks source link

error: unreported exception Throwable #32

Closed axsr-snt closed 1 year ago

axsr-snt commented 1 year ago

Na layer de implementação aparece um erro relacionado ao orElseThrow:

   @Override
    public Sacola verSacola(Long sacolaId) {
        return sacolaRepository.findById(sacolaId).orElseThrow(
                () -> {
                    throw new RuntimeException("A sacola não foi encontrada");
                }
        );
    }
    @Override
    public Sacola fecharSacola(Long sacolaId, int formaPagamentoId) {
        Sacola sacola = verSacola(sacolaId);
        if (sacola.getItens().isEmpty()) {
            throw new RuntimeException("Insira itens na sacola.");
        }
        FormaPagamento formaPagamento =
                formaPagamentoId == 0 ?
                        FormaPagamento.DINHEIRO : FormaPagamento.MAQUINETA;
        sacola.setFormaPagamento(formaPagamento);
        sacola.setFechada(true);
        return sacolaRepository.save(sacola);
    }
}

error: unreported exception Throwable; must be caught or declared to be thrown

a solução que encontrei foi explicitar Throwable nos métodos que possuiam o orElseThrow

`    @Override
    public Sacola verSacola(Long sacolaId) throws Throwable {
        return sacolaRepository.findById(sacolaId).orElseThrow(
                () -> {
                    throw new RuntimeException("A sacola não foi encontrada");
                }
        );
    }

    @Override
    public Sacola fecharSacola(Long sacolaId, int formaPagamentoId) throws Throwable {
        Sacola sacola = verSacola(sacolaId);
        if (sacola.getItens().isEmpty()) {
            throw new RuntimeException("Insira itens na sacola.");
        }
        FormaPagamento formaPagamento =
                formaPagamentoId == 0 ?
                        FormaPagamento.DINHEIRO : FormaPagamento.MAQUINETA;
        sacola.setFormaPagamento(formaPagamento);
        sacola.setFechada(true);
        return sacolaRepository.save(sacola);
    }
}`

gostaria de saber se essa é a solução correta e o possivel motivo pelo qual o meu código apresentou essa necessidade?

axsr-snt commented 1 year ago

Consegui identificar o erro, meu ItemId estava como String e não como Long