package Ġcom . posts . api . error Ċ Ċ import Ġorg . springframework . dao . Duplicate Key Exception Ċ import Ġorg . springframework . http . HttpHeaders Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . http . Response Entity Ċ import Ġorg . springframework . web . Http MediaType NotSupportedException Ċ import Ġorg . springframework . web . bind . Method Argument Not Valid Exception Ċ import Ġorg . springframework . web . bind . annotation . Controller Ad vice Ċ import Ġorg . springframework . web . bind . annotation . ExceptionHandler Ċ import Ġorg . springframework . web . context . request . WebRequest Ċ import Ġorg . springframework . web . server . ResponseStatus Exception Ċ import Ġorg . springframework . web . servlet . mvc . method . annotation . Response Entity ExceptionHandler Ċ import Ġjavax . validation . Constraint Violation Exception Ċ Ċ /** Ċ Ġ* ĠGlobal Ġhandler Ġof Ġapplication Ġexceptions Ġwith Ġmapping Ġto Ġerror Ġmessages Ġand Ġcodes . Ċ Ġ*/ Ċ @ Controller Ad vice Ċ class ĠError Handler Ġ: ĠResponseEntity ExceptionHandler () Ġ{ ĊĊĠ Ġ@ ExceptionHandler ( Field Exception :: class ) ĊĠ Ġfun Ġhandle Field Exceptions ( ex : ĠField Exception ): ĠResponseEntity < Any > Ġ= ĊĠĠĠ ĠResponseEntity ( ex . errors , ĠHttpStatus . BAD _ REQUEST ) ĊĊĠ Ġ@ ExceptionHandler ( Data NotFoundException :: class ) ĊĠ Ġfun Ġhandle Data NotFoundException s ( ex : ĠData NotFoundException ): ĠResponseEntity < Any > Ġ= ĊĠĠĠ ĠResponseEntity ( ex . error , ĠHttpStatus . NOT _ FOUND ) ĊĊĠ Ġ@ ExceptionHandler ( ServiceException :: class ) ĊĠ Ġfun Ġhandle Service Exceptions ( ex : ĠService Exception ): ĠResponseEntity < Any > Ġ= ĊĠĠĠ ĠResponseEntity ( ex . error , ĠHttpStatus . BAD _ REQUEST ) ĊĊĠ Ġ@ ExceptionHandler ( ResponseStatus Exception :: class ) ĊĠ Ġfun Ġhandle Global Exceptions ( ex : ĠResponse Status Exception ): ĠResponseEntity < String > Ġ= ĊĠĠĠ ĠResponseEntity ( ex . message , Ġex . status ) ĊĊĠ Ġ@ ExceptionHandler ( Duplicate Key Exception :: class ) ĊĠ Ġfun Ġhandle Duplicate Key Exception ( ex : ĠDuplicate Key Exception ?): ĠResponseEntity < String > Ġ= ĊĠĠĠ ĠResponseEntity (" Error Ġwith Ġunique Ġconstraint .", ĠHttpStatus . BAD _ REQUEST ) ĊĊĠ Ġ@ ExceptionHandler ( Constraint Violation Exception :: class ) ĊĠ Ġfun Ġhandle Constraint Violation Exception ( ex : ĠConstraint Violation Exception ): ĠResponseEntity < String > Ġ= ĊĠĠĠ ĠResponseEntity (" Error Ġwith Ġconstraint Ġviolation : Ġ" Ġ+ Ġex . message , ĠHttpStatus . BAD _ REQUEST ) ĊĊĠ Ġoverride Ġfun Ġhandle Http MediaType NotSupported ( ĊĠĠĠ Ġex : ĠHttp MediaType NotSupportedException , ĊĠĠĠ Ġheaders : ĠHttp Headers , ĊĠĠĠ Ġstatus : ĠHttpStatus , ĊĠĠĠ Ġrequest : ĠWeb Request , ĊĠ Ġ): ĠResponseEntity < Any > Ġ= ĠResponseEntity ( ĊĠĠĠ Ġ" Error Ġwith Ġmedia Ġtype Ġnot Ġsupported : Ġ" Ġ+ Ġex . message , ĊĠĠĠ ĠHttpStatus . UNSUPPORTED _ MEDIA _ TYPE ĊĠ Ġ) ĊĊĠ Ġoverride Ġfun Ġhandle Method Argument Not Valid ( ĊĠĠĠ Ġex : ĠMethod Argument Not Valid Exception , ĊĠĠĠ Ġheaders : ĠHttp Headers , ĊĠĠĠ Ġstatus : ĠHttpStatus , ĊĠĠĠ Ġrequest : ĠWeb Request , ĊĠ Ġ): ĠResponseEntity < Any > Ġ{ ĊĠĠĠ Ġval Ġerrors : ĠMutable Map < String , ĠString ?> Ġ= Ġmutable Map Of () ĊĠĠĠ Ġfor Ġ( error Ġin Ġex . binding Result . field Errors ) Ġ{ ĊĠĠĠĠĠ Ġerrors [ error . field ] Ġ= Ġerror . default Message ĊĠĠĠ Ġ} ĊĠĠĠ Ġreturn ĠResponseEntity ( errors , ĠHttpStatus . BAD _ REQUEST ) ĊĠ Ġ} Ċ }