Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 53 additions & 7 deletions sip-codec/src/main/java/com/sip/codec/SipCodecException.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,68 @@
package com.sip.codec;

import java.util.Objects;

/**
* Thrown when wire-format bytes cannot be parsed into a valid SIP message,
* or when a message cannot be serialized.
*
* <p>The codec aims to be tolerant of common deviations described in
* RFC 5118 / RFC 4475, but malformed input that prevents safe interpretation
* must surface as an exception.</p>
* <p>Each exception carries a {@linkplain #category() stable category} that
* groups failure modes (e.g. {@code malformed-start-line},
* {@code malformed-header}, {@code unknown-version}). The category is the
* same vocabulary that fixture {@code .expect.properties} files declare,
* so the compliance harness can assert end-to-end behaviour without coupling
* to free-form messages.</p>
*
* <p>The {@linkplain #offset() byte offset} points to the location in the
* input that triggered the failure (or {@code -1} when no specific offset
* applies, e.g. for trailing-data failures).</p>
*/
public class SipCodecException extends RuntimeException {

private static final long serialVersionUID = 1L;

public SipCodecException(String message) {
super(message);
/** Stable, lower-kebab-case taxonomy. Extend as new failure modes appear. */
public static final class Category {
public static final String MALFORMED_START_LINE = "malformed-start-line";
public static final String MALFORMED_HEADER = "malformed-header";
public static final String UNKNOWN_VERSION = "unknown-version";
public static final String BAD_CONTENT_LENGTH = "bad-content-length";
public static final String TRUNCATED = "truncated";
public static final String UNSUPPORTED_URI_SCHEME = "unsupported-uri-scheme";
public static final String ENCODE_FAILURE = "encode-failure";

private Category() { }
}

private final String category;
private final int offset;

public SipCodecException(String category, int offset, String message) {
super(formatMessage(category, offset, message));
this.category = Objects.requireNonNull(category, "category");
this.offset = offset;
}

public SipCodecException(String category, int offset, String message, Throwable cause) {
super(formatMessage(category, offset, message), cause);
this.category = Objects.requireNonNull(category, "category");
this.offset = offset;
}

/** Stable taxonomy bucket; see {@link Category} for known values. */
public String category() {
return category;
}

/** Byte offset of the failure within the input, or {@code -1} if unknown. */
public int offset() {
return offset;
}

public SipCodecException(String message, Throwable cause) {
super(message, cause);
private static String formatMessage(String category, int offset, String message) {
if (offset < 0) {
return "[" + category + "] " + message;
}
return "[" + category + " @ byte " + offset + "] " + message;
}
}
Loading
Loading