Skip to content

Commit

Permalink
Add InvalidCookieError
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharuja committed Jan 29, 2020
1 parent 5904687 commit 2dc1c20
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
21 changes: 7 additions & 14 deletions stdlib/http/src/main/ballerina/src/http/cookie/cookie.bal
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,28 @@ public type Cookie object {
}

// Returns true if the attributes of the cookie are in the correct format or else error is returned.
public function isValid() returns boolean|error {
CookieHandlingError err;
public function isValid() returns boolean|InvalidCookieError {
var name = self.name;
if (name is string) {
name = name.trim();
if (name == "") {
err = error(COOKIE_HANDLING_ERROR, message = "Invalid name");
return err;
return error(INVALID_COOKIE_ERROR, message = "Invalid name: Name cannot be empty");
}
self.name = name;
}
var value = self.value;
if (value is string) {
value = value.trim();
if (value == "") {
err = error(COOKIE_HANDLING_ERROR, message = "Invalid value");
return err;
return error(INVALID_COOKIE_ERROR, message = "Invalid value: Value cannot be empty");
}
self.value = value;
}
var domain = self.domain;
if (domain is string) {
domain = domain.trim().toLowerAscii();
if (domain == "") {
err = error(COOKIE_HANDLING_ERROR, message = "Invalid domain");
return err;
return error(INVALID_COOKIE_ERROR, message = "Invalid domain: Domain cannot be empty");
}
if (domain.startsWith(".")) {
domain = domain.substring(1, domain.length());
Expand All @@ -98,22 +94,19 @@ public type Cookie object {
if (path is string) {
path = path.trim();
if (path == "" || !path.startsWith("/") || stringutils:contains(path, "?")) {
err = error(COOKIE_HANDLING_ERROR, message = "Path is not in correct format");
return err;
return error(INVALID_COOKIE_ERROR, message = "Invalid path: Path is not in correct format");
}
self.path = path;
}
var expires = self.expires;
if (expires is string) {
expires = expires.trim();
if (!toGmtFormat(self, expires)) {
err = error(COOKIE_HANDLING_ERROR, message = "Time is not in correct format");
return err;
return error(INVALID_COOKIE_ERROR, message = "Invalid time: Expiry-time is not in yyyy-mm-dd hh:mm:ss format");
}
}
if (self.maxAge < 0) {
err = error(COOKIE_HANDLING_ERROR, message = "Max Age can not be less than zero");
return err;
return error(INVALID_COOKIE_ERROR, message = "Invalid max-age: Max Age can not be less than zero");
}
return true;
}
Expand Down
7 changes: 6 additions & 1 deletion stdlib/http/src/main/ballerina/src/http/http_errors.bal
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ public const WRITING_100_CONTINUE_RESPONSE_FAILED = "{ballerina/http}Writing100C
# Represents an error that occurred while writing 100 continue response
public type Writing100ContinueResponseError error<WRITING_100_CONTINUE_RESPONSE_FAILED, Detail>;

# Represents the reason string for the `http:InvalidCookieError`
public const INVALID_COOKIE_ERROR = "{ballerina/http}InvalidCookieError";
# Represents a cookie error that occurred when sending cookies in the response
public type InvalidCookieError error<INVALID_COOKIE_ERROR, Detail>;

// Generic errors (mostly to wrap errors from other modules)
# Error reason for generic client error
public const GENERIC_CLIENT_ERROR = "{ballerina/http}GenericClientError";
Expand Down Expand Up @@ -202,7 +207,7 @@ public type InboundRequestError InitializingInboundRequestError|ReadingInboundRe
# Defines the listener error types that returned while sending outbound response
public type OutboundResponseError InitializingOutboundResponseError|WritingOutboundResponseHeadersError|
WritingOutboundResponseBodyError|Initiating100ContinueResponseError|
Writing100ContinueResponseError|CookieHandlingError;
Writing100ContinueResponseError|InvalidCookieError;

# Defines the possible client error types
public type ClientError ResiliencyError|ClientAuthError|OutboundRequestError|
Expand Down

0 comments on commit 2dc1c20

Please sign in to comment.