Skip to content

Commit

Permalink
Merge pull request #216 from linea-it/208-bug-pz-server-instability
Browse files Browse the repository at this point in the history
Fixes #208 - Timeout has been increased and error handling.
  • Loading branch information
jandsonrj authored Dec 15, 2023
2 parents 4f63d5c + 0fe0be2 commit 3f3ea96
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
65 changes: 30 additions & 35 deletions frontend/components/ProductDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ export default function ProductDetail({ productId, internalName }) {
setProduct(res)
setLoading(false)
})
.catch(res => {
.catch(error => {
setLoading(false)
if (res.response.status === 500) {
// TODO: Tratar erro
setNotFound(true)
if (error.response && error.response.status === 500) {
console.error('Internal Server Error:', error.response.data)
} else {
console.error('Error loading product by ID:', error.message)
}
})
}, [productId])
Expand All @@ -82,38 +83,30 @@ export default function ProductDetail({ productId, internalName }) {

const loadProductByName = React.useCallback(async () => {
setLoading(true)
getProducts({ filters: { internal_name: internalName } })
.then(res => {
// Apresenta a interface de Produtos
if (res.results.length === 1) {
setLoading(false)
setProduct(res)
}
if (res.results.length === 0) {
setNotFound(true)
}
})
.catch(res => {
setLoading(false)
if (res.response.status === 500) {
// TODO: Tratar erro
setNotFound(true)
}

try {
const res = await getProducts({
filters: { internal_name: internalName }
})

getProducts({ filters: { internal_name: internalName } })
.then(res => {
// Apresenta a interface de Produtos
if (res.results.length === 1) {
setProduct(res.results[0])
}
if (res.results.length === 1) {
setLoading(false)
})
.catch(res => {
// Retorna error 404
// TODO: Tratar os errors e apresentar.
setProduct(res.results[0])
} else if (res.results.length === 0) {
setLoading(false)
})
setNotFound(true)
}
} catch (error) {
setLoading(false)

if (error.response && error.response.status === 404) {
console.error('Product not found:', error.response.data)
} else if (error.response && error.response.status === 500) {
console.error('Internal Server Error:', error.response.data)
} else {
console.error('Error loading product by name:', error.message)
}
}
}, [internalName])

React.useEffect(() => {
Expand Down Expand Up @@ -146,7 +139,9 @@ export default function ProductDetail({ productId, internalName }) {
.catch(error => {
setLoading(false)
if (error.response && error.response.status === 500) {
// TODO: Tratar erro
console.error('Internal Server Error:', error.response.data)
} else {
console.error('Error loading product files:', error.message)
}
})
}, [product])
Expand All @@ -169,8 +164,8 @@ export default function ProductDetail({ productId, internalName }) {
link.click()
setDownloading(false)
})
.catch(() => {
// TODO: Tratar erro no download
.catch(error => {
console.error('Error downloading file:', error.message)
setDownloading(false)
})
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { refreshToken } from './auth'
export function getAPIClient(ctx) {
const api = axios.create({
// baseURL: '/api',
timeout: 15000,
timeout: 30000,
headers: {
'Content-Type': 'application/json',
accept: 'application/json'
Expand Down

0 comments on commit 3f3ea96

Please sign in to comment.