Commit 20d2453f authored by Diego Molteni's avatar Diego Molteni
Browse files

added reason for 423 error message

parent 061e6c2b
Pipeline #40062 failed
......@@ -130,8 +130,7 @@ paths:
409:
description: "Conflict"
423:
description: "Locked"
description: "Locked. The error message contains the Reason in the form <b>[RCODE:REASON(2-char-code)TTL(sec-number)]</b>. Possible Reasons code are: <ul><li>WL(Write Locked)</li><li>RL(Read Locked)</li><li>CL(Cannot be Locked)</li><li>UL(Cannot be Unlocked)</ul>\nFor example the reason code <b>[RCODE:RL3600]</b> indicates the dataset is Read Locked with a TTL of 3600sec"
get:
summary: "Retrieve a dataset"
description: "<ul><li>Return the dataset metadata from seismic store.</li><li>Required roles: subproject.admin, subproject.editor, subproject.viewer</li></ul>"
......@@ -270,7 +269,7 @@ paths:
409:
description: "Conflict"
423:
description: "Locked"
description: "Locked. The error message contains the Reason in the form <b>[RCODE:REASON(2-char-code)TTL(sec-number)]</b>. Possible Reasons code are: <ul><li>WL(Write Locked)</li><li>RL(Read Locked)</li><li>CL(Cannot be Locked)</li><li>UL(Cannot be Unlocked)</ul>\nFor example the reason code <b>[RCODE:RL3600]</b> indicates the dataset is Read Locked with a TTL of 3600sec"
/dataset/tenant/{tenantid}/subproject/{subprojectid}/dataset/{datasetid}/lock:
put:
......@@ -324,7 +323,7 @@ paths:
404:
description: "Not found"
423:
description: "Locked"
description: "Locked. The error message contains the Reason in the form <b>[RCODE:REASON(2-char-code)TTL(sec-number)]</b>. Possible Reasons code are: <ul><li>WL(Write Locked)</li><li>RL(Read Locked)</li><li>CL(Cannot be Locked)</li><li>UL(Cannot be Unlocked)</ul>\nFor example the reason code <b>[RCODE:RL3600]</b> indicates the dataset is Read Locked with a TTL of 3600sec"
/dataset/tenant/{tenantid}/subproject/{subprojectid}/dataset/{datasetid}/unlock:
put:
......@@ -366,7 +365,7 @@ paths:
404:
description: "Not found"
423:
description: "Locked"
description: "Locked. The error message contains the Reason in the form <b>[RCODE:REASON(2-char-code)TTL(sec-number)]</b>. Possible Reasons code are: <ul><li>WL(Write Locked)</li><li>RL(Read Locked)</li><li>CL(Cannot be Locked)</li><li>UL(Cannot be Unlocked)</ul>\nFor example the reason code <b>[RCODE:RL3600]</b> indicates the dataset is Read Locked with a TTL of 3600sec"
/dataset/tenant/{tenantid}/subproject/{subprojectid}/dataset/{datasetid}/permission:
get:
......@@ -747,7 +746,7 @@ paths:
409:
description: "Conflict"
423:
description: "Locked"
description: "Locked. The error message contains the Reason in the form <b>[RCODE:REASON(2-char-code)TTL(sec-number)]</b>. Possible Reasons code are: <ul><li>WL(Write Locked)</li><li>RL(Read Locked)</li><li>CL(Cannot be Locked)</li><li>UL(Cannot be Unlocked)</ul>\nFor example the reason code <b>[RCODE:RL3600]</b> indicates the dataset is Read Locked with a TTL of 3600sec"
/utility/gcs-access-token:
get:
......
......@@ -343,7 +343,8 @@ export class DatasetHandler {
if (Locker.isWriteLock(await Locker.getLock(lockKey))) {
throw (Error.make(Error.Status.LOCKED,
'The dataset ' + Config.SDPATHPREFIX + datasetIn.tenant + '/' +
datasetIn.subproject + datasetIn.path + datasetIn.name + ' is write locked'));
datasetIn.subproject + datasetIn.path + datasetIn.name + ' is write locked ' +
Error.get423WriteLockReason()));
}
}
......@@ -441,7 +442,8 @@ export class DatasetHandler {
if (Locker.isWriteLock(await Locker.getLock(lockKey))) {
throw (Error.make(Error.Status.LOCKED,
'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
datasetIN.subproject + datasetIN.path + datasetIN.name + ' is write locked'));
datasetIN.subproject + datasetIN.path + datasetIN.name + ' is write locked ' +
Error.get423WriteLockReason()));
}
}
......@@ -857,7 +859,8 @@ export class DatasetHandler {
if (Locker.isWriteLock(await Locker.getLock(lockKey))) {
throw (Error.make(Error.Status.LOCKED,
'The dataset ' + Config.SDPATHPREFIX + datasetIN.tenant + '/' +
datasetIN.subproject + datasetIN.path + datasetIN.name + ' is write locked'));
datasetIN.subproject + datasetIN.path + datasetIN.name + ' is write locked ' +
Error.get423WriteLockReason()));
}
}
......
......@@ -37,6 +37,10 @@ export class Locker {
private static redisSubscriptionClient;
private static redlock;
public static getWriteLockTTL(): number { return this.EXP_WRITELOCK };
public static getReadLockTTL(): number { return this.EXP_READLOCK };
public static getMutexTTL(): number { return this.TTL };
public static async init() {
if (Config.UTEST) {
......@@ -179,7 +183,9 @@ export class Locker {
}
throw (Error.make(Error.Status.LOCKED,
lockKey + ' is ' + (this.isWriteLock(lockValue) ? 'write' : 'read') + ' locked'));
lockKey + ' is ' + (this.isWriteLock(lockValue) ?
('write locked ') + Error.get423WriteLockReason():
('read locked ' + + Error.get423ReadLockReason()))));
}
// remove both lock and mutex
......@@ -215,7 +221,7 @@ export class Locker {
if (lockValue && wid && wid !== lockValue && this.isWriteLock(lockValue)) {
await this.releaseMutex(cachelock, lockKey);
throw (Error.make(Error.Status.LOCKED,
lockKey + ' is locked for write with different id'));
lockKey + ' is locked for write with different id ' + Error.get423WriteLockReason()));
}
// ------------------------------------------------
......@@ -239,20 +245,22 @@ export class Locker {
if (!wid) {
await this.releaseMutex(cachelock, lockKey);
throw (Error.make(Error.Status.LOCKED,
lockKey + ' is locked for ' + (this.isWriteLock(lockValue) ? 'write' : 'read')));
lockKey + ' is locked for ' + (this.isWriteLock(lockValue) ?
'write ' + Error.get423WriteLockReason() :
'read ' + + Error.get423ReadLockReason())));
}
// write locked and different wid
if (this.isWriteLock(lockValue) && wid !== lockValue) {
await this.releaseMutex(cachelock, lockKey);
throw (Error.make(Error.Status.LOCKED,
lockKey + ' is locked for write with different id'));
lockKey + ' is locked for write with different id ' + Error.get423WriteLockReason()));
}
if (!this.isWriteLock(lockValue) && lockValue.indexOf(wid) === -1) {
await this.releaseMutex(cachelock, lockKey);
throw (Error.make(Error.Status.LOCKED,
lockKey + ' is locked for read widh different ids'));
lockKey + ' is locked for read with different ids ' + + Error.get423ReadLockReason()));
}
// Trusted Open
......@@ -286,14 +294,14 @@ export class Locker {
if (!wid) {
await this.releaseMutex(cachelock, lockKey);
throw (Error.make(Error.Status.LOCKED,
lockKey + ' is locked for write'));
lockKey + ' is locked for write ' + Error.get423WriteLockReason()));
}
// wid different -> error different wid
if (wid !== lockValue) {
await this.releaseMutex(cachelock, lockKey);
throw (Error.make(Error.Status.LOCKED,
lockKey + ' is locked for write with different wid'));
lockKey + ' is locked for write with different wid ' + Error.get423WriteLockReason()));
}
// wid match -> TRUSTED OPEN
......@@ -304,7 +312,7 @@ export class Locker {
if (lockValue && wid && lockValue.indexOf(wid) === -1) {
await this.releaseMutex(cachelock, lockKey);
throw (Error.make(Error.Status.LOCKED,
lockKey + ' is locked for read with different ids'));
lockKey + ' is locked for read with different ids ' + Error.get423ReadLockReason()));
}
// ------------------------------------------------
......@@ -466,7 +474,9 @@ export class Locker {
const cachelock = await this.redlock.lock('locks:' + key, this.TTL);
return cachelock;
} catch (error) {
throw Error.make(Error.Status.LOCKED, key + ' cannot be locked at the moment. Please try again shortly.');
throw Error.make(Error.Status.LOCKED, key +
' cannot be locked at the moment. Please try again shortly. ' +
Error.get423CannotLockReason());
}
}
......@@ -476,7 +486,9 @@ export class Locker {
try {
await this.redlock.unlock(cachelock);
} catch (error) {
throw Error.make(Error.Status.LOCKED, key + ' cannot be unlocked at the moment.');
throw Error.make(Error.Status.LOCKED, key +
' cannot be unlocked at the moment. ' +
Error.get423CannotUnlockReason());
}
}
......
......@@ -14,6 +14,8 @@
// limitations under the License.
// ============================================================================
import { Locker } from '../services/dataset/locker';
export class ErrorModel {
public error: {
code: number,
......@@ -35,6 +37,7 @@ export class Error {
NOT_IMPLEMENTED: 501
};
public static make(errorCode: number, message: string, mexprefix: string = '[seismic-store-service]'): ErrorModel {
return {
error: {
......@@ -60,4 +63,35 @@ export class Error {
return Object.keys(this.Status).find((key) => this.Status[key] === value);
}
// ---------------------------------------
// Error code 423 Reason
// ---------------------------------------
private static Reason423 = {
WRITE_LOCK: 'WL',
READ_LOCK: 'RL',
CANNOT_LOCK: 'CL',
CANNOT_UNLOCK: 'CU'
}
private static create423Reason(stringCode: string, ttl: number): string {
return '[RCODE: ' + stringCode + ':' + ttl + ']';
}
public static get423WriteLockReason(): string {
return this.create423Reason(this.Reason423.WRITE_LOCK, Locker.getWriteLockTTL());
}
public static get423ReadLockReason(): string {
return this.create423Reason(this.Reason423.READ_LOCK, Locker.getReadLockTTL());
}
public static get423CannotLockReason(): string {
return this.create423Reason(this.Reason423.CANNOT_LOCK, Locker.getMutexTTL());
}
public static get423CannotUnlockReason(): string {
return this.create423Reason(this.Reason423.CANNOT_UNLOCK, Locker.getMutexTTL())
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment