-
적절한 Serialization 방식 선택하기운영 중인 서비스/Coconut. 2024. 4. 22. 19:51
이번에 새로 추가한 기능을 배포한 후에 이슈가 발생해서 급하게 수정을 하고 기록으로 남겨봅니다.
현재 유저가 입력한 답변을 외부 api를 사용해서 채점을 하고 있습니다.
외부 api가 요구하는 형태의 JSON을 String.format을 사용하여 아래와 같이 생성하고 있었습니다.
서비스 운영 중에 한 사용자가 종종 오류가 발생한다는 것을 제보해주셨습니다.
에러 메세지를 확인해보니 요청에 전달하는 JSON에 문제가 있는 듯 합니다.
"message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly.)
아쉬웠던 부분은 예외 처리를 하면서 유저 인풋에 문제가 있을 때 로그를 남겨두지 않았던 것이었습니다.
앞으로 인풋을 처리해야하는 곳에서는 예외 발생시 꼭 입력값을 로그로 남길 수 있도록해야겠습니다.
급하게 로그를 추가하여 새로 배포를 진행하였고, 이어서 계속 테스트를 하던 중 문제의 원인을 찾을 수 있었습니다.
답변의 내용에 newline 이 추가되면 문제가 발생하는 것을 확인할 수 있었습니다.
자바에서 문자열을 다룰 때 일부 특별한 문자들은 \ 를 사용하여 escape처리 필요한데, 누락되어서 발생하는 문제였습니다.
케이스를 잡아서 escape 처리를 하기보다는 있는 라이브러리를 사용하는 편이 나을 것 같아 Jackson을 사용합니다.
따로 메소드로 분리했지만 역시나 처음에 사용한 방법에 비해 조금 장황한 느낌이 듭니다. 하지만 여러 엣지 케이스들을 신경써도 되지 않아도되는 장점이 너무 큽니다 ㅎㅎ
이번에 확실히 기억하고 가야할 것은 아래 2가지인 듯 합니다.
1. 유저 인풋을 받는 경우에는 유저 인풋을 로그로 남긴다.
2. 자바에서 JSON을 만들어야하면 라이브러리를 쓴다.
감사합니다.
'운영 중인 서비스 > Coconut.' 카테고리의 다른 글
커다란 만능 모듈을 사용하면 만날 수 있는 함정 (0) 2024.04.30 왜 멀티모듈 프로젝트로 변경하려고 하는가? (0) 2024.04.26 무중단 배포 유지하면서 EC2 교체 하기 (0) 2024.04.19 단일 진입점으로 개별 서비스의 Swagger 접근하기 (0) 2024.04.13 Nginx 로그 살펴보기 ( feat. 웹 스캐닝 ) (0) 2024.04.07