본문 바로가기

개발기술/Java

Deep Dive into 디버깅

주체적인 디버깅의 필요성

  추상적으로 이해하고 있는 프레임워크나 라이브러리가 구체적으로 어떻게 동작하고 있는지 이해하기 위해서는 프로그래밍의 흐름을 Line By Line으로 이해하는 것이 필수적이다. 코드의 이해에서 가장 동기부여가 되는 상황은 에러발생상황 해결일 때가 많다. 단순히 구글링이나 GPT에 알아보는 것도 방안이지만, 스스로 이해의 증진이라는 관점에서 주체적인 디버깅은 필수불가결이다. 

Step 1: Analyze the Error Log

  1. Look at the Error Message:
    • The error message often gives a clue about what went wrong. It might indicate a NullPointerException, a validation failure, or a specific exception related to your logic.

Error Message:

  • What It Is: The error message is a brief statement that describes what went wrong. It usually contains a specific exception or error type (e.g., NullPointerException, PatternParseException), and sometimes it includes a short description of the problem.
  • Location: The error message typically appears at the beginning of the log output when an exception is thrown.
org.springframework.web.util.pattern.PatternParseException: No more pattern data allowed after {*...} or ** pattern element

 

 

2. Examine the Stack Trace:

    1. The stack trace is a list of method calls that the program was executing when the error occurred. Identify the first method in your codebase (as opposed to third-party libraries) where the error occurred. This is usually the best place to start investigating. Based on the stack trace, find the class and method where the error first occurred. This is where you should consider setting your first breakpoint.

Stack Trace:

  • Top of the Stack Trace: This is where the exception originated, not where it was lastly handled. The first few lines show the initial cause of the error.
  • Bottom of the Stack Trace: This is where the exception was last caught or handled in your code.

at org.springframework.web.util.pattern.InternalPathPatternParser.peekDoubleWildcard(InternalPathPatternParser.java:250) ~[spring-web-6.1.12.jar:6.1.12]
at org.springframework.web.util.pattern.InternalPathPatternParser.parse(InternalPathPatternParser.java:113) ~[spring-web-6.1.12.jar:6.1.12]
at org.springframework.web.util.pattern.PathPatternParser.parse(PathPatternParser.java:129) ~[spring-web-6.1.12.jar:6.1.12]

 

 

  1. Determine the Relevant Class and Method:
    • Step 2: Identify the Suspected Issue

Depending on the error message and the stack trace, you might be dealing with issues like:

  • NullPointerException: Indicates that your code is trying to use an object reference that is null.
  • Validation Errors: Could be related to incorrect input data or missing fields.
  • Repository/Database Errors: Might involve issues with querying or saving data.
  • External API Errors: Could be due to failed API calls, network issues, or improper request formats.

Step 3: Set Breakpoints

    • 에러지점을 파악하고서 그것과 관련된 Layer에 BreakPoint를 걸어서 문제를 확인해본다. 그 곳은 Controller, Service, Repository, 또는 Configuration이 될 수도있다. 

Step 4: Run and Debug

  1. Run the Application in Debug Mode:
    • Start your application in debug mode, which allows you to pause execution at your breakpoints and inspect the state of the application.
    • Step through the code, line by line if necessary, to observe the behavior and identify where things go wrong.
  2. Inspect Variables and Method Returns:
    • While paused at a breakpoint, examine the values of variables and the results of method calls. Look for anything unexpected, such as null values or incorrect data.
  3. Iterate Based on Findings:
    • If you find the issue, fix it and test again.
    • If not, move your breakpoints to other suspected areas based on what you learned during the initial debugging.