Created
March 20, 2024 14:10
-
-
Save semihkeskindev/7c7f1333f5ada44078899f4ed3ae3f67 to your computer and use it in GitHub Desktop.
Print 422 Error messages came from laravel endpoints on frontend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {message} from 'antd' | |
export const errorMessage = (error: any, printErrorMessage: boolean = true) => { | |
if (!error.response) { | |
return | |
} | |
let response = error.response | |
let data = response.data | |
let statusCode = response.status | |
const defaultMessage = 'Bir şeyler ters gitti. Lütfen daha sonra tekrar dene.' | |
let msg = '' | |
if (403 === statusCode) { | |
return | |
} | |
if (422 === statusCode) { | |
if (typeof data === 'object' && data !== null && data.hasOwnProperty('errors')) { | |
if (typeof data.errors === 'object') { | |
Object.keys(data.errors).forEach((key, index) => { | |
msg = data.errors[key][0] | |
}) | |
} | |
} | |
msg += ' Girdiğin bilgileri kontrol edip tekrar dene.' | |
} | |
if (printErrorMessage) { | |
const willBePrintedMessage = msg || defaultMessage | |
if (willBePrintedMessage) { | |
message.error(willBePrintedMessage) | |
} | |
} | |
return msg ? msg : null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment